我想将Java. util. date对象转换为Java中的字符串。
格式为2010-05-30 22:15:52
我想将Java. util. date对象转换为Java中的字符串。
格式为2010-05-30 22:15:52
当前回答
单镜头;)
获取日期
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
为了得到时间
String time = new SimpleDateFormat("hh:mm", Locale.getDefault()).format(new Date());
来获取日期和时间
String dateTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefaut()).format(new Date());
快乐编码:)
其他回答
public static String formateDate(String dateString) {
Date date;
String formattedDate = "";
try {
date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",Locale.getDefault()).parse(dateString);
formattedDate = new SimpleDateFormat("dd/MM/yyyy",Locale.getDefault()).format(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return formattedDate;
}
看起来您正在寻找SimpleDateFormat。
格式:yyyy-MM-dd kk:mm:ss
单线选项
该选项通过简单的一行来编写实际的日期。
请注意,这是使用Calendar.class和SimpleDateFormat,然后它不是 在Java8下使用它是合乎逻辑的。
yourstringdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
Date date = new Date();
String strDate = String.format("%tY-%<tm-%<td %<tH:%<tM:%<tS", date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = "2010-05-30 22:15:52";
java.util.Date formatedDate = sdf.parse(date); // returns a String when it is parsed
System.out.println(sdf.format(formatedDate)); // the use of format function returns a String