DateFormat的使用

DateFormat

引入包

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

使用方式

定义日期时间格式
格式化日期时间参数

Date转换为String

比如
2017-04-19 星期三 下午 20:17:38

Date date = new Date();
DateFormat bf = new SimpleDateFormat("yyyy-MM-dd E a HH:mm:ss");
String format = bf.format(date);

或者
2017-04-19

SimpleDateFormat bf = new SimpleDateFormat("yyyy-MM-dd");
String format = bf.format(new Date());

String转换为Date

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
    String fromDate =(String)param.get("fromDate");
    Date sdate = sdf.parse(fromDate);
} catch (ParseException e) {
    e.printStackTrace();
}

版权声明:本文为nangeali原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。