Mic*_*zka 22
您可以使用的SimpleDateFormat使用yyyy-MM-dd HH:mm:ss,并明确设置时区:
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"))));
}
打印出来:
2010-11-17 13:12:00 +0100
2010-11-17 20:12:00 +0100
更新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"))));
哪个打印 2010-11-17 13:12:00 +0530
Bas*_*que 11
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'.
其他答案使用麻烦的旧日期时间类(Date,Calendar,等),现在的传统,由java.time类取代.
LocalDateTime我有一个字符串yyyy-MM-dd hh:mm a
这样的输入字符串没有任何偏离UTC或时区的指示.所以我们解析为LocalDateTime.
定义格式模式以使输入与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
请注意,LocalDateTime是不是一个特定的时刻,只有各种可能的时刻,一个模糊的概念.例如,法国巴黎午夜过后几分钟,加拿大蒙特利尔仍然是"昨天".因此,如果没有时区的背景,Europe/Paris或者America/Montreal只是说"午夜后的几分钟"没有任何意义.
ZoneId我可以单独获取时区对象,其中上面的字符串表示日期.
时区由ZoneId班级表示.
指定适当的时区名称,格式continent/region,如America/Montreal,Africa/Casablanca或Pacific/Auckland.切勿使用3-4字母缩写,例如EST或IST因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!).
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime应用ZoneId得到一个ZonedDateTime确实是时间轴上的一个点,这是历史上的一个特定时刻.
ZonedDateTime zdt = ldt.atZone( z );
zdt.toString():2017-01-23T12:34-05:00 [美国/蒙特利尔]
Instant我想将其转换为以下格式.yyyy-MM-dd HH:mm:ss Z.
首先,要知道Z文字字符是简称Zulu并且意味着UTC.换句话说,零点小时的UTC偏移,+00:00.
该Instant级表示时间轴上的时刻UTC,分辨率为纳秒(最多9个(9)小数的位数).
您可以从中提取Instant对象ZonedDateTime.
Instant instant = zdt.toInstant();  // Extracting the same moment but in UTC.
要生成标准ISO 8601格式的字符串,例如2017-01-22T18:21:13.354Z,调用toString.标准格式没有空格,使用a T将年 - 月 - 日与小时 - 分 - 秒分开,并将Z规范附加为零偏移.
String output = instant.toString();
instant.toString():2017-01-23T17:34:00Z
我强烈建议尽可能使用标准格式.如果您坚持按照所述的所需格式使用空格,请在DateTimeFormatter对象中定义自己的格式化模式,或者只对输出执行字符串操作Instant::toString.
String output = instant.toString()
                       .replace( "T" , " " )  // Substitute SPACE for T.
                       .replace( "Z" , " Z" ); // Insert SPACE before Z.
输出:2017-01-23 17:34:00 Z
在IdeOne.com上试用此代码.
该java.time框架是建立在Java 8和更高版本.这些类取代麻烦的老传统日期时间类,如java.util.Date,Calendar,和SimpleDateFormat.
现在处于维护模式的Joda-Time项目建议迁移到java.time类.
要了解更多信息,请参阅Oracle教程.并搜索Stack Overflow以获取许多示例和解释.规范是JSR 310.
从哪里获取java.time类?
该ThreeTen-额外项目与其他类扩展java.time.该项目是未来可能添加到java.time的试验场.您可以在此比如找到一些有用的类Interval,YearWeek,YearQuarter,和更多.
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);
| 归档时间: | 
 | 
| 查看次数: | 79220 次 | 
| 最近记录: |