Java 将带毫秒的日期转换为其他日期格式
我将日期作为字符串传递,该字符串的日期格式为毫秒,如下所示:Java 将带毫秒的日期转换为其他日期格式,java,calendar,simpledateformat,Java,Calendar,Simpledateformat,我将日期作为字符串传递,该字符串的日期格式为毫秒,如下所示: String dateMilli = "2014-08-21 09:11:44.187"; 如何使用此格式将此格式转换为日期格式: yyyy-MM-dd'T'HH:mm:ss or 2014-08-21T10:39:41 到目前为止我有这个,但它不起作用 Date tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(dateMilli); DateFo
String dateMilli = "2014-08-21 09:11:44.187";
如何使用此格式将此格式转换为日期格式:
yyyy-MM-dd'T'HH:mm:ss
or
2014-08-21T10:39:41
到目前为止我有这个,但它不起作用
Date tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(dateMilli);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
dateFormat.format(tempDate);
System.out.println(" date format: "+ dateFormat.toString());
Output:
date format: java.text.SimpleDateFormat@6b2ed43a
打印
DateFormat
对象的结果时,需要将格式应用于Date
并将其打印出来
String formatted = dateFormat.format(tempDate);
System.out.println(" date format: "+ formatted);
打印
DateFormat
对象的结果时,需要将格式应用于Date
并将其打印出来
String formatted = dateFormat.format(tempDate);
System.out.println(" date format: "+ formatted);
您正在打印DateFormat对象--不要这样做!您需要使用DateFormat对象将字符串解析为日期,或者将日期格式化为字符串,然后打印返回的字符串。请查看API,了解解析和格式化方法 您需要两个SimpleDataFormat对象:
- 用于将第一种格式的字符串转换为日期的字符串。
- 您可以通过调用格式的
方法来完成parse(String s)
- 您可以通过调用格式的
- 另一个SimpleDataFormat将用于将日期格式化为第二种类型的字符串:
- 您可以通过调用格式的
方法来实现这一点format(dated)
- 然后简单地打印出返回的字符串
- 您可以通过调用格式的
- 您不应该尝试打印DateFormat对象本身,因为这毫无意义
- 用于将第一种格式的字符串转换为日期的字符串。
- 您可以通过调用格式的
方法来完成parse(String s)
- 您可以通过调用格式的
- 另一个SimpleDataFormat将用于将日期格式化为第二种类型的字符串:
- 您可以通过调用格式的
方法来实现这一点format(dated)
- 然后简单地打印出返回的字符串
- 您可以通过调用格式的
- 您不应该尝试打印DateFormat对象本身,因为这毫无意义
- 您正在打印DateFormat对象--不要这样做!您需要使用DateFormat对象将字符串解析为日期,或者将日期格式化为字符串,然后打印返回的字符串。请查看API,了解解析和格式化方法
您需要两个SimpleDataFormat对象: