[英]How to format current string into a date/time format?
I am trying to convert this String into date/time format我正在尝试将此字符串转换为日期/时间格式
I am getting this string from the database and just want to convert it into proper date and time...我从数据库中获取此字符串,只想将其转换为正确的日期和时间...
String str = 2019-02-22T13:43:00Z;//input String str = 2019-02-22T13:43:00Z;//输入
I am getting proper date from this string by this code:我通过此代码从此字符串中获取正确的日期:
String[] split_date = date_time.split("T",2);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = format.parse ( split_date[0]);
date_time = new SimpleDateFormat("dd-MMM-yyyy hh:mm", Locale.ENGLISH).format(date1);
In above code (date_time = 22-Feb-2019 12:00) is coming.在上面的代码中(date_time = 22-Feb-2019 12:00)即将到来。
expected output is : 22-feb-2019 13:43预期输出为:2019 年 2 月 22 日 13:43
The problem here is I can't get the proper time from that default format.这里的问题是我无法从默认格式中获得正确的时间。
The modern approach uses the java.time classes.现代方法使用java.time类。
Instant instant = Instant.parse( "2019-02-22T13:43:00Z" ) ;
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MMM-uuuu HH:mm" ) ;
String output = odt.format( f ) ;
output :输出 :
22-Feb-2019 13:43
The java.time framework is built into Java 8 and later. java.time框架内置于 Java 8 及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
, Calendar
, & SimpleDateFormat
.这些类取代麻烦的老传统日期时间类,如
java.util.Date
, Calendar
,和SimpleDateFormat
。
To learn more, see the Oracle Tutorial .要了解更多信息,请参阅Oracle 教程。 And search Stack Overflow for many examples and explanations.
并在 Stack Overflow 上搜索许多示例和解释。 Specification is JSR 310 .
规范是JSR 310 。
The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.现在处于维护模式的Joda-Time项目建议迁移到java.time类。
You may exchange java.time objects directly with your database.您可以直接与您的数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later.
使用符合JDBC 4.2或更高版本的JDBC 驱动程序。 No need for strings, no need for
java.sql.*
classes.不需要字符串,不需要
java.sql.*
类。
Where to obtain the java.time classes?从哪里获得 java.time 类?
The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目用额外的类扩展了 java.time。 This project is a proving ground for possible future additions to java.time.
该项目是未来可能添加到 java.time 的试验场。 You may find some useful classes here such as
Interval
, YearWeek
, YearQuarter
, and more .您可以在这里找到一些有用的类,比如
Interval
, YearWeek
, YearQuarter
,和更多。
Try below code:试试下面的代码:
String dt = "2019-02-22T13:43:00Z";
SimpleDateFormat mainformat = new SimpleDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'", Locale.getDefault());
mainformat.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
Date date1 = mainformat.parse(dt);
String date_time = new SimpleDateFormat("dd-MMM-yyyy HH:mm", Locale.ENGLISH).format(date1);
Log.e("Date Time", date_time);
} catch (Exception e) {
e.printStackTrace();
}
Output: 22-Feb-2019 13:43
输出: 2019 年 2 月 22 日 13:43
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.