简体   繁体   中英

Problems with Date manipulation

I am getting 2 timestamps from the Oracle database start and end dates. They come in this format 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

In the above output I am interested in the ----dateparse--- value as I need this for comparison. 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. 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

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.

Assume:

  1. Date objects are retrieved from the database
  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.

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.

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. For example, on my PC it displays it in British Summer Time, the current time zone in the UK.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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