日期在java代码中使用的太多了,不过很多时候得到的是国际时间,而国际时间并不符合我们的要求,这里提供一个可以将String或者Date类型的变量转换为我们所需的标准格式时间。
public class test{
public static void main(String[] args) throws Exception{
Format fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date today = new Date();
System.out.println("今天是:" + fm.format(today));
Calendar c = Calendar.getInstance();
c.setTime(today);
c.add(Calendar.DAY_OF_MONTH, 1);// 在今天的基础上+1天/周/月
Date tomorrow = c.getTime();
System.out.println("明天是:" + fm.format(tomorrow));
String a="2013-12-02 21:22:22";
Date d=sdf.parse(a);
System.out.println("转换之前的时间:"+d);
System.out.println("转换之后的时间:"+fm.format(d));
}
}
对String类型的变量,先用SimpleDateFormat对象转成date类型,再用Format转为标准格式
结果:
今天是:2013-10-09
明天是:2013-10-10