20191031-Date与String、Long三者的转换

mac2024-05-17  30

<一>. date 与 string 类型的转换: 1.date => string

Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String dateStr = dateFormat.format(date); System.out.println(dateStr);

2.string => date

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); String s= "2011-07-09 "; //时间格式要一致 Date date = dateFormat.parse(s);

<二>. date 与long类型转换 1.date => long

long milliSeconds = 1571814689000l; Date date = new Date(); date.setTime(milliSeconds);

2.long => date

Date date = new Date(); long dateLong = date.getTime();
最新回复(0)