简体   繁体   English

Java 将日期时间转换为给定格式

[英]Java Convert dateTime to the given format

I need some help to convert the given local date time value (or util date) to one of the below formats with (ASIA/COLOMBO) time zone.我需要一些帮助来将给定的本地日期时间值(或实用日期)转换为具有(ASIA/COLOMBO)时区的以下格式之一。 Here are the formats (from one-signal API).以下是格式(来自单信号 API)。

"Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)" “2015 年 9 月 24 日星期四 14:00:00 GMT-0700 (PDT)”

"September 24th 2015, 2:00:00 pm UTC-07:00" “2015 年 9 月 24 日,下午 2:00:00 UTC-07:00”

"2015-09-24 14:00:00 GMT-0700" “2015-09-24 14:00:00 GMT-0700”

"Sept 24 2015 14:00:00 GMT-0700" “2015 年 9 月 24 日 14:00:00 GMT-0700”

"Thu Sep 24 2015 14:00:00 GMT-0700 (Pacific Daylight Time)" “2015 年 9 月 24 日星期四 14:00:00 GMT-0700(太平洋夏令时间)”

Thanks.谢谢。

ZonedDateTime

Instantiate a date, time, and zone into a ZonedDateTime object.将日期、时间和区域实例化为ZonedDateTime object。

LocalDate ld = LocalDate.of( 2015 , Month.SEPTEMBER , 24 ) ;
LocalTime lt = LocalTime.of( 14 , 0 ) ;
ZoneId z = ZoneId.of( "Asia/Colombo" ) ;
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;

If given a java.util.Date object, convert to a Instant .如果给定java.util.Date object,则转换为Instant

Instant instant = myJavaUtilDate.toInstant() ;

Apply a zone to move from UTC.应用区域以从 UTC 移动。

ZoneId z = ZoneId.of( "Asia/Colombo" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

To produce text in your desired formats, use DateTimeFormatter class.要生成所需格式的文本,请使用DateTimeFormatter class。 This has been addressed many many times already on Stack Overflow.这个问题已经在 Stack Overflow 上解决了很多次。 So search for that class name to learn more.因此,搜索该 class 名称以了解更多信息。

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd HH:mm:ss OOOO" ) ;
String output = zdt.format( f ) ;

2015-09-24 14:00:00 GMT+05:30 2015-09-24 14:00:00 GMT+05:30

ISO 8601 ISO 8601

Those formats you showed are all terrible choices.你展示的那些格式都是糟糕的选择。 When exchanging date-time values as text, use standard ISO 8601 formats.将日期时间值交换为文本时,请使用标准ISO 8601格式。 The standard formats are designed to be easy to parse by machine, and easy to read by humans across cultures.标准格式旨在易于机器解析,并且易于跨文化的人类阅读。

The java.time classes use ISO 8601 formats by default when parsing/generating strings. java.time类在解析/生成字符串时默认使用 ISO 8601 格式。

The java.time classes were introduced in 1.8 and allows for more flexible and concise code. java.time 类是在 1.8 中引入的,允许更灵活和简洁的代码。

Instant now = Instant.now();
ZonedDateTime dateTime = now.atZone(ZoneId.of("Asia/Colombo"));
DateTimeFormatter format = DateTimeFormatter.ofPattern("E M d u HH:mm:ss OOOO (z)");

// Using a format to convert at Temporal to a string
String formattedDate = format.format(dateTime);

// Using a format to convert a string to a Instant
Instant parsedDate = Instant.from(format.parse(formattedDate));

You can specify your date's format using DateTimeFormatter.ofPattern or one of the many constants defined in DateTimeFormatter.您可以使用DateTimeFormatter.ofPattern或 DateTimeFormatter 中定义的众多常量之一指定日期的格式。 The pattern I created matches your first example. 我创建的模式与您的第一个示例相匹配。

The old method老方法

These classes are yet to be deprecated and I find to be more straight forward for simple cases than the modern method.这些类还没有被弃用,我发现对于简单的情况比现代方法更直接。

java.text.SimpleDateFormat; java.text.SimpleDateFormat; It can handle going to and from a String and java.util.Date Object.它可以处理往返字符串和 java.util.Date Object。

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss Z (z)");
format.setTimeZone(TimeZone.getTimeZone("Asia/Colombo"));
// Note that the current date and time will be used when instantiating a new Date object.
String formattedDate = format.format(new Date());

try {
    Date parsedDate = format.parse(formattedDate);
} catch (ParseException exception) {
    // Handle malformed date
}

You will just have to create the pattern for any format you'd like to use.您只需为您想使用的任何格式创建模式

EEE MMM dd yyyy HH:mm:ss Z (z) = Thu Sep 24 2015 14:00:00 GMT-0700 (PDT) EEE MMM dd yyyy HH:mm:ss Z (z) = Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM