我从数据库中获取时间为2013-11-01 00:00:00这是一个字符串。 我想使用java将其转换为2013年11月1日。 怎么做?
答案 0 :(得分:3)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
Date d = sdf.parse(input);
sdf.applyPattern("dd MM yyyy");
String output = sdf.format(d);
答案 1 :(得分:0)
试试这种方式
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateInString = "2013-11-01 00:00:00";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
根据以下评论
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateInString = "2013-11-01 00:00:00";
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = formatter.parse(dateInString);
//System.out.println(date);
//System.out.println(formatter.format(date));
DateFormat out = new SimpleDateFormat("dd MMM yyyy");
System.out.println(out.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
输出 2013年11月1日