简体   繁体   English

日期操作问题

[英]Problems with Date manipulation

I am getting 2 timestamps from the Oracle database start and end dates. 我从Oracle数据库的开始和结束日期获得2个时间戳。 They come in this format yyyy-MM-dd HH:mm:ss.SSS 它们的格式为yyyy-MM-dd HH:mm:ss.SSS

Now I am getting another timestamp for comparison from a text file. 现在,我从文本文件中获取另一个时间戳进行比较。 It's in String format. 它是字符串格式。 Below is the code that I have written to extract the required info. 下面是我编写的用于提取所需信息的代码。

    DateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy");
    Date date = f.parse("Tue Aug 23 20:00:03 PDT 2011");
    System.out.println("---date----" + f.format(date));
    String originalDate = f.format(date);
    System.out.println("---originalDate----" + originalDate);

    DateFormat f2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    String dateParse = f2.format(date);
    System.out.println("---dateParse----"+dateParse);

The output that I am getting is a little strange and is very inconsistent. 我得到的输出有些奇怪,并且非常不一致。 Below is sample ---date----Tue Aug 23 20:00:03 PDT 2011 ---originalDate----Tue Aug 23 20:00:03 PDT 2011 ---dateParse----2011-08-24 08:30:03.000 以下是示例-日期-2011年8月23日星期二20:00:03 --- originalDate ---- 2011年8月23日2月23日太平洋夏令时间--dateParse ---- 2011-08 -24 08:30:03.000

In the above output I am interested in the ----dateparse--- value as I need this for comparison. 在上面的输出中,我对---- dateparse ---值感兴趣,因为需要进行比较。 Now if you see the values are varying in the ideal case both should be same value. 现在,如果您看到值在理想情况下有所变化,则两个值应相同。

The issue here is if the output varies with the timezone then how am I supposed to do comparison in the code from the data that is fetched from the database? 这里的问题是,如果输出随时区变化,那么我应该如何对从数据库中获取的数据中的代码进行比较?

I just need to see if --dateparse-- lies between the date ranges that I get from db. 我只需要查看--dateparse--是否位于从数据库获取的日期范围之间即可。 Will this code work properly independent of the timezone or there is some issue here. 此代码是否可以独立于时区正常工作,或者此处存在一些问题。 If the date is varying then my comparison can go wrong or is it the correct way with the above 2 outputs. 如果日期是变化的,那么我的比较可能会出错,或者上述两个输出是正确的方法。

Can you please let me know how I can resolve this issue. 您能告诉我如何解决此问题。

Please refer to the class documentation and use the constructor: 请参考类文档并使用构造函数:

http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

SimpleDateFormat(String pattern, Locale locale)

To check your locale, use the following code: 要检查您的语言环境,请使用以下代码:

Locale.getDefault()

Probably that's why you're getting these results. 也许这就是为什么您得到这些结果。

I just need to see if --dateparse-- lies between the date ranges that i get from db. 我只需要查看--dateparse--是否位于我从数据库获取的日期范围之间即可。

Assume: 假设:

  1. Date objects are retrieved from the database 从数据库中检索Date对象
  2. Timezone information is embedded in the file format being parse 时区信息以正在解析的文件格式嵌入

Then all instances of Date have the proper absolute value (counting milliseconds since Jan 1, 1970) and thus compare correctly. 然后,所有Date实例都具有正确的绝对值(自1970年1月1日起,以毫秒为单位),因此可以正确比较。

So what should you watch out for? 那么,您应该注意什么呢? A date/time string with no Timezone or Locale information. 没有时区或语言环境信息的日期/时间字符串。 If you were to parse one of these, your Locale and Timezone will be used which may not be the same as when/where the data was written. 如果要解析其中之一,则将使用“区域设置”和“时区”,它们可能与写入数据的时间/位置不同。 In this case, the retrieved Date may reference the wrong absolute time. 在这种情况下,检索到的Date可能会引用错误的绝对时间。

If you add a time zone to your second date format, you'll notice it outputs it using your default locale, as barbosa pointed out. barbosa所指出的,如果您在第二个日期格式中添加了时区,则会使用默认语言环境将其输出。 For example, on my PC it displays it in British Summer Time, the current time zone in the UK. 例如,在我的PC上,它以英国当前时区英国夏令时显示它。

DateFormat f2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS zz");
System.out.println("---dateParse----" + f2.format(date));

Output: 输出:

---dateParse----2011-08-24 04:00:03.000 BST

If you want to force the output time zone of the date format, you can do this using the setTimeZone method: 如果要强制使用日期格式的输出时区,可以使用setTimeZone方法执行此操作:

f2.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));

The output then becomes: 输出将变为:

---dateParse----2011-08-23 20:00:03.000 PDT

Just like Locale , TimeZone has a getDefault method, if necessary. 就像Locale一样,如果需要, TimeZone也具有getDefault方法。

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

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