尝试指定日期,进行日期格式转换, 代码如下:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MMM/yyyy hh:mm:ss", Locale.ENGLISH);
return simpleDateFormat.format(new Date(2018,2,1)) ;
//返回 01/Mar/3918 12:00:00
这里返回的日期完全不对, 查看文档后发现, Date类构造函数的各项参数并不是指定公元日期, 而是距1900年的偏移量。
year – the year minus 1900.
month – the month between 0-11.
date – the day of the month between 1-31.
更正后的代码:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MMM/yyyy hh:mm:ss", Locale.ENGLISH);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss", Locale.ENGLISH);
return simpleDateFormat.format(simpleDateFormat2.parse("2018/03/05 01:01:01")) ;
//返回 05/Mar/2018 01:01:01
另外需要注意的是M是指月份,m是指分钟, 不要把M误写成m