简体   繁体   English

使用时区将字符串转换为日期

[英]Converting string to date with timezone

I have a string in the pattern yyyy-MM-dd hh:mm a and i can get the time zone object separately in which the above string represents the date. 我在模式yyyy-MM-dd hh:mm a中有一个字符串,我可以单独获取时区对象,其中上面的字符串表示日期。

I want to convert this to the below format. 我想将其转换为以下格式。 yyyy-MM-dd HH:mm:ss Z yyyy-MM-dd HH:mm:ss Z.

How can i do this? 我怎样才能做到这一点?

You can use SimpleDateFormat with yyyy-MM-dd HH:mm:ss and explicitly set the TimeZone : 您可以将SimpleDateFormatyyyy-MM-dd HH:mm:ss并显式设置TimeZone

public static Date getSomeDate(final String str, final TimeZone tz)
    throws ParseException {
  final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
  sdf.setTimeZone(tz);
  return sdf.parse(str);
}

/**
 * @param args
 * @throws IOException
 * @throws InterruptedException
 * @throws ParseException
 */
public static void main(final String[] args) throws ParseException {
  final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
  System.out.println(sdf.format(getSomeDate(
      "2010-11-17 01:12 pm", TimeZone.getTimeZone("Europe/Berlin"))));
  System.out.println(sdf.format(getSomeDate(
      "2010-11-17 01:12 pm", TimeZone.getTimeZone("America/Chicago"))));
}

Prints out: 打印出来:

2010-11-17 13:12:00 +0100 2010-11-17 13:12:00 +0100

2010-11-17 20:12:00 +0100 2010-11-17 20:12:00 +0100

Update 2010-12-01: If you want to explicitly printout a special TimeZone, set it in the SimpleDateFormat: 更新2010-12-01:如果要显式打印出特殊的TimeZone,请在SimpleDateFormat中进行设置:

sdf.setTimeZone(TimeZone .getTimeZone("IST")); 
System.out.println(sdf.format(getSomeDate(
    "2010-11-17 01:12 pm", TimeZone.getTimeZone("IST"))));

Which prints 2010-11-17 13:12:00 +0530 其中打印2010-11-17 13:12:00 +0530

tl;dr TL;博士

LocalDateTime.parse(                        // Parse string as value without time zone and without offset-from-UTC.
    "2017-01-23 12:34 PM" , 
    DateTimeFormatter.ofPattern( "uuuu-MM-dd hh:mm a" )
)                                           // Returns a `LocalDateTime` object.
.atZone( ZoneId.of( "America/Montreal" ) )  // Assign time zone, to determine a moment. Returns a `ZonedDateTime` object.
.toInstant()                                // Adjusts from zone to UTC.
.toString()                                 // Generate string: 2017-01-23T17:34:00Z
.replace( "T" , " " )                       // Substitute SPACE for 'T' in middle.
.replace( "Z" , " Z" )                      // Insert SPACE before 'Z'.

Avoid legacy date-time classes 避免遗留日期时间类

The other Answers use the troublesome old date-time classes ( Date , Calendar , etc.), now legacy, supplanted by the java.time classes. 其他Answers使用麻烦的旧日期时间类( DateCalendar等),现在是旧的,由java.time类取代。

LocalDateTime

I have a string in the pattern yyyy-MM-dd hh:mm a 我有一个字符串yyyy-MM-dd hh:mm a

Such an input string lacks any indication of offset-from-UTC or time zone. 这样的输入字符串没有任何偏离UTC或时区的指示。 So we parse as a LocalDateTime . 所以我们解析为LocalDateTime

Define a formatting pattern to match your input with a DateTimeFormatter object. 定义格式模式以使您的输入与DateTimeFormatter对象匹配。

String input = "2017-01-23 12:34 PM" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd hh:mm a" );
LocalDateTime ldt = LocalDateTime.parse( input , f );

ldt.toString(): 2017-01-23T12:34 ldt.toString():2017-01-23T12:34

Note that a LocalDateTime is not a specific moment, only a vague idea about a range of possible moments. 请注意, LocalDateTime 不是特定时刻,只是对一系列可能时刻的模糊概念。 For example, a few minutes after midnight in Paris France is still “yesterday” in Montréal Canada. 例如,法国巴黎午夜过后几分钟,加拿大蒙特利尔仍然是“昨天”。 So without the context of a time zone such as Europe/Paris or America/Montreal , just saying “a few minutes after midnight” has no meaning. 因此,如果没有Europe/ParisAmerica/Montreal等时区的背景,只说“午夜后几分钟”就没有意义了。

ZoneId

and i can get the time zone object separately in which the above string represents the date. 我可以单独获取时区对象,其中上面的字符串表示日期。

A time zone is represented by the ZoneId class. 时区由ZoneId类表示。

Specify a proper time zone name in the format of continent/region , such as America/Montreal , Africa/Casablanca , or Pacific/Auckland . continent/region的格式指定适当的时区名称 ,例如America/MontrealAfrica/CasablancaPacific/Auckland Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!). 切勿使用3-4字母缩写,例如ESTIST因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "America/Montreal" );

ZonedDateTime

Apply the ZoneId to get a ZonedDateTime which is indeed a point on the timeline, a specific moment in history. 应用ZoneId获取ZonedDateTime ,这确实是时间轴上的一个点,也就是历史中的特定时刻。

ZonedDateTime zdt = ldt.atZone( z );

zdt.toString(): 2017-01-23T12:34-05:00[America/Montreal] zdt.toString():2017-01-23T12:34-05:00 [美国/蒙特利尔]

Instant

I want to convert this to the below format. 我想将其转换为以下格式。 yyyy-MM-dd HH:mm:ss Z yyyy-MM-dd HH:mm:ss Z.

First, know that a Z literal character is short for Zulu and means UTC . 首先,要知道Z lite字符是Zulu缩写,意思是UTC In other words, an offset-from-UTC of zero hours, +00:00 . 换句话说, 零时,+ +00:00 的UTC偏移

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction). Instant类表示UTC时间轴上的一个时刻,分辨率为纳秒 (最多九(9)位小数)。

You can extract a Instant object from a ZonedDateTime . 您可以从ZonedDateTime提取Instant对象。

Instant instant = zdt.toInstant();  // Extracting the same moment but in UTC.

To generate a string in standard ISO 8601 format, such as 2017-01-22T18:21:13.354Z , call toString . 要生成标准ISO 8601格式的字符串,例如2017-01-22T18:21:13.354Z ,请调用toString The standard format has no spaces, uses a T to separate the year-month-date from the hour-minute-second, and appends the Z canonically for an offset of zero. 标准格式没有空格,使用T将年 - 月 - 日与小时 - 分 - 秒分开,并将Z规范地附加到零偏移。

String output = instant.toString();

instant.toString(): 2017-01-23T17:34:00Z instant.toString():2017-01-23T17:34:00Z

I strongly suggest using the standard formats whenever possible. 我强烈建议尽可能使用标准格式。 If you insist on using spaces as in your stated desired format, either define your own formatting pattern in a DateTimeFormatter object or just do a string manipulation on the output of Instant::toString . 如果您坚持使用所述格式的空格,请在DateTimeFormatter对象中定义自己的格式设置模式,或者只对Instant::toString的输出执行字符串操作。

String output = instant.toString()
                       .replace( "T" , " " )  // Substitute SPACE for T.
                       .replace( "Z" , " Z" ); // Insert SPACE before Z.

output: 2017-01-23 17:34:00 Z 输出:2017-01-23 17:34:00 Z

Try this code live at IdeOne.com . 在IdeOne.com上试用此代码


About java.time 关于java.time

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.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

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

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 . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

Use SimpleDateFormat 使用SimpleDateFormat

String string1 = "2009-10-10 12:12:12 ";
SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z")
sdf.setTimeZone(tz);
Date date = sdf.parse(string1);

Undoubtedly, the format which is generally used will be of a form 2014-10-05T15:23:01Z (TZ) 毫无疑问,通常使用的格式为2014-10-05T15:23:01Z (TZ)

For that one has to use this code 为此,必须使用此代码

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String dateInString = "2014-10-05T15:23:01Z";

 try {

     Date date = formatter.parse(dateInString.replaceAll("Z$", "+0000"));
     System.out.println(date);

 } catch (ParseException e) {
     e.printStackTrace();
 }

Its output will be Sun Oct 05 20:53:01 IST 2014 它的输出将是2014年10月5日20:53:01

However, I am not sure why we had to replaceAll "Z" if you do not add replaceAll the program will fail. 但是,我不知道为什么我们不得不替换所有“Z”如果你不添加replaceAll程序将失败。

Create a new instance of SimpleDateFormat using your date pattern. 使用日期模式创建SimpleDateFormat的新实例。 Afterwards you can call it's parse method to convert date strings to a java.util.Date object. 之后,您可以调用它的parse方法将日期字符串转换为java.util.Date对象。

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

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