问题描述
import java.sql.Timestamp;import java.text.SimpleDateFormat;import java.util.Locale;public class CommonUtil { public static Timestamp getDate(){ Timestamp time=new Timestamp(System.currentTimeMillis()); System.out.println("time: "+time); SimpleDateFormat sdf= new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); String timeStr=sdf.format(time); System.out.println("timeStr: "+timeStr); return time; } public static void main(String[] args){ System.out.println(getDate()); }}输出结果:time: 2013-03-27 22:11:59.244timeStr: 2013-11-27 10:11:592013-03-27 22:11:59.244这里月份出现错误,是什么原因?
解决方案
Timestamp time=new Timestamp(System.currentTimeMillis()); System.out.println("time: "+time); SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String timeStr=sdf.format(time); System.out.println("timeStr: "+timeStr); return time; mm-MM hh-HH
解决方案二:
没仔细看api么 月份的M是大写的
解决方案三:
MM是月份,mm是分钟
解决方案四:
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
解决方案五:
应该是yyyy-MM-dd HH:mm:ss
解决方案六:
yyyy-mm-dd hh:mm:ss改为yyyy-MM-dd HH:mm:ss