日历和 SimpleDateFormat 输出错误的日期
Calender and SimpleDateFormat outputting the wrong date
每当我执行此代码时
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("D:M:Y");
System.out.println("Date Launched: " + "[" + sdf.format(cal.getTime()) + "]");
打印出
Date Launched: [285:10:2015]
我怎样才能得到它以便打印出像 [12/10/2015]
这样的实际日期?
使用
SimpleDateFormat sdf = new SimpleDateFormat("d/M/Y");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");//d small
您设置的格式有误。
而不是:
SimpleDateFormat sdf = new SimpleDateFormat("D:M:Y");
使用:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
其中:
y Year Year 1996; 96
Y Week year Year 2009; 09
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
使用 dd/MM/yyyy 而不是 D:M:Y 所以你的代码看起来像
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("Date Launched: " + "[" + sdf.format(cal.getTime()) + "]");
因为 D 给出的是年中的日,而你想要的是月中的日
M给出年份中的月份
Y 给出年份
试试下面的代码
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
符号'd'表示月中的第几天
符号'D'表示一年中的第几天
每当我执行此代码时
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("D:M:Y");
System.out.println("Date Launched: " + "[" + sdf.format(cal.getTime()) + "]");
打印出
Date Launched: [285:10:2015]
我怎样才能得到它以便打印出像 [12/10/2015]
这样的实际日期?
使用
SimpleDateFormat sdf = new SimpleDateFormat("d/M/Y");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");//d small
您设置的格式有误。
而不是:
SimpleDateFormat sdf = new SimpleDateFormat("D:M:Y");
使用:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
其中:
y Year Year 1996; 96
Y Week year Year 2009; 09
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
使用 dd/MM/yyyy 而不是 D:M:Y 所以你的代码看起来像
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("Date Launched: " + "[" + sdf.format(cal.getTime()) + "]");
因为 D 给出的是年中的日,而你想要的是月中的日
M给出年份中的月份
Y 给出年份
试试下面的代码
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
符号'd'表示月中的第几天
符号'D'表示一年中的第几天