简体   繁体   English

Java日期格式问题

[英]Java Date Format Issue

I have the number of seconds from 31 Dec 1969 19:00:00 ie (EST) and I want to convert the same to a date format. 我有从1969年12月31日19:00:00开始的秒数,即(EST),我想将其转换为日期格式。

I have the following code which returns any invalid date. 我有以下代码,它返回任何无效的日期。 Can anyone point out what seems to be the issue here. 任何人都可以指出这里的问题所在。

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


public class sample 
{

    public static void main(String args[])
    {
        DateFormat df = new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss");
        df.setTimeZone(TimeZone.getTimeZone("EST"));
        Date d1 = new Date(1000*1373604190); //Converting to millisecond
         String formattedDate = df.format(d1); 
         System.out.println(formattedDate); //I'm getting 22/Dec/1969 16:50:55
    }
}

How can I solve this. 我该如何解决。

You are using int literals instead of long in your numbers. 您正在使用int文字而不是long在你的号码。 By default a integral literal is int unless you specify that is long with L at the end. 默认情况下,整数文字为int除非您指定结尾为L的long Try this: 尝试这个:

Date d1 = new Date(1000*1373604190L);

(note the L at the end of your literal 1373604190) (请注意,您的文字1373604190的末尾是L)

tl;dr tl; dr

Instant.ofEpochSeconds( 1_373_604_190L ) 
       .atZone( ZoneId.of( "America/New_York" ) )
       .format( DateTimeFormatter.ofPattern( "dd MMM uuuu HH:mm:ss" , Locale.CANADA ) )

Details 细节

the number of seconds from 31 Dec 1969 19:00:00 ie (EST) 从1969年12月31日19:00:00开始的秒数,即(EST)

Do not think parochially when doing date-time work. 在进行日期时间工作时,请不要轻描淡写。 Rather than translate values such as the epoch reference moment in and out of your own zone, just think in UTC . 而不是在诸如此类的值转换为进入和离开您自己的区域的历元参考时刻时,只需考虑UTC即可 Consider UTC to be The One True Time , all others being variations on that theme. 可以 UTC视为唯一的真实时间 ,所有其他时间都是该主题的变体。

So, always refer to the epoch reference moment as 1970-01-01T00:00:00Z , the first moment of 1970 in UTC, rather than in any other time zone. 因此,始终将纪元参考时刻称为1970-01-01T00:00:00Z ,这是UTC 1970年的第一时刻,而不是任何其他时区。

(EST) (美东时间)

Also, never use those 3-4 letter abbreviations as time zones. 另外,切勿使用3-4个字母的缩写作为时区。 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 Those 3-4 letter abbreviation such as EST or IST are not true time zones, not standardized, and not even unique(!). 诸如ESTIST这样的3-4个字母的缩写不是真实的时区,不是标准化的,甚至不是唯一的(!)。

Using java.time 使用java.time

The modern way to do this work is with the java.time classes. 完成这项工作的现代方法是使用java.time类。

long secondsSinceEpoch = 1_373_604_190L ;

We convert that to a Instant object. 我们将其转换为Instant对象。 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)位数字)。

Instant instant = Instant.ofEpochSeconds( secondsSinceEpoch ) ;

To see this value through the lens of some wall-clock time , apply a time zone as a ZoneId to get a ZonedDateTime object. 要通过某个壁钟时间查看此值,请应用时区作为ZoneId以获取ZonedDateTime对象。

I am guessing that by EST you meant the time zone used by much of the eastern portions of the United States and Canada. 我猜想, EST是指美国和加拿大许多东部地区使用的时区。 I will arbitrarily choose the time zone America/New_York . 我将任意选择时区America/New_York

ZoneId z = ZoneId.of( "America/New_York" );
ZonedDateTime zdt = instant.atZone( "America/New_York" );

secondsSinceEpoch: 1373604190 秒始于史诗:1373604190

instant.toString(): 2013-07-12T04:43:10Z Instant.toString():2013-07-12T04:43:10Z

zdt.toString(): 2013-07-12T00:43:10-04:00[America/New_York] zdt.toString():2013-07-12T00:43:10-04:00 [美国/纽约]

See live code at IdeOne.com . 请参阅IdeOne.com上的实时代码

Strings 弦乐

To generate a string, you can either let java.time automatically localize or you can specify an explicit formatting pattern. 要生成一个字符串,可以使java.time自动本地化,也可以指定一个显式的格式化模式。

Locale locale = Locale.CANADA ;
DateTimeFormatter formatterLocalized = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM ).withLocale( locale );
String outputLocalized = zdt.format( formatterLocalized );

12-Jul-2013 12:43:10 AM 2013年7月12日上午12:43:10

DateTimeFormatter formatterCustom = DateTimeFormatter.ofPattern( "dd MMM uuuu HH:mm:ss" , locale );
String output = zdt.format( formatterCustom );

12 Jul 2013 00:43:10 2013年7月12日00:43:10


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 ,和更多

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

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