1

There is the date date1 given with the format YY-MM-dd hh:mm:ss.SSS I want to compare

date1.getTime()

with one retrieved by doing

new Date().getTime()

There is

SimpleDateFormat sf = new SimpleDateFormat("YY-MM-dd hh:mm:ss.SSS");
Date date1 = sf.parse(date1AsString);
...
compare date1.getTime() with new Date().getTime();

How can I bring these two dates to a common 'timezone' to compare them? How can I obtain date1 to be on the same 'time length' as new Date()? I want to have the same timezone... Thanks

Roxana
  • 1,525
  • 3
  • 22
  • 39
  • 3
    A `Date` does not have a timezone. – Sotirios Delimanolis Dec 16 '14 at 16:55
  • ok, but how can I compare these values? – Roxana Dec 16 '14 at 16:55
  • 1
    `new Date().compareTo(date1)` perhaps? It's not clear what you're trying to achieve... – Jon Skeet Dec 16 '14 at 16:57
  • They are two `long` values. Do you know how to compare numerical values? – Sotirios Delimanolis Dec 16 '14 at 16:57
  • if I do new Date().compareTo(date1) will this be done on the same timezone? – Roxana Dec 16 '14 at 17:00
  • 2
    **A `Date` does not have a timezone.** – Sotirios Delimanolis Dec 16 '14 at 17:00
  • but how does simpledateformat parse the string if not by using a timezone? – Roxana Dec 16 '14 at 17:01
  • `date1AsString` must contain timezone information otherwise you cannot compare them. – Salman A Dec 16 '14 at 17:26
  • @Roxana Both of your questions have been confused. I suggest you search StackOverflow for "java date" and do some study. Your questions may come into focus once you become familiar with how date-time computation works. Key ideas: • A java.util.Date object has no time zone yet it's `toString` method applies the JVM’s current default time zone (yes, confusing and misleading). • The java.util.Date/.Calendar/java.text.SimpleDateFormat classes are notoriously troublesome and confusing. Avoid them and use either [Joda-Time](http://www.joda.org/joda-time/) or java.time instead. – Basil Bourque Dec 16 '14 at 22:33

7 Answers7

1

From the javadoc of Date

The class Date represents a specific instant in time, with millisecond precision.

An instance in time is agnostic of our definition of time enhanced with time zones. Right now is the same for you and me, regardless of the fact that we are (potentially) in different time zones.

What adds the notion of a time zone is the DateFormat

The date is represented as a Date object or as the milliseconds since January 1, 1970, 00:00:00 GMT.

When you invoke Date#getTime(), you get back

the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

This is something you can use to compare Date objects since they have the same root. Similarly, the compareTo will return

the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument.

Sotirios Delimanolis
  • 263,859
  • 56
  • 671
  • 702
1

You cannot compare two dates if their timezone information is unknown.

For example if your date1AsString variable is simply 2014-12-16 16:00:00 then you cannot tell if it is greater than or less than 2014-12-16 20:00:00+0000.

It looks like first date is 4 hours less than second one; but if someone adds that the first date is Pacific Time (UTC-0800) then it would actually be 4 hours more than the second date (2014-12-16 16:00:00-0800 = 2014-12-17 00:00:00+0000).

So, if date1AsString has an unknown timezone then you cannot convert it to UTC or anything else for comparison.

Salman A
  • 248,760
  • 80
  • 417
  • 510
0

getTime() Returns the number of milliseconds since January 1, 1970, 00:00 :00. You will want to compare the two longs, roughly like so:

long time = new Date().getTime();
long time2 = date1.getTime();
if(time>time2)
    System.out.println(String.format("time is greater than time2 by %d", time-time2)
else
    System.out.println(String.format("time2 is greater than time by %d", time2-time)
MeetTitan
  • 3,148
  • 1
  • 12
  • 22
  • Timezone wise, how do you know which timezone to use? Date1 for instance may be EST already and you may try converting it to EST again skewing your results. If you have both timezones stored with the dates, simply do some arithmetic to determine the difference in time between timezones. – MeetTitan Dec 16 '14 at 17:15
0

if you know Timezone date1.setTime(date1.getTime()+/-(3600000*hours)) to convert, if date1 time is GMT to go to GMT+2 hours must be 2 and sing must be +

nodasn
  • 13
  • 5
0

Typically any server environment is going to be using GMT so you wont have to worry about the timezone when comparing dates, only when formatting the value to be display to users.

You can compare two dates like so:

SimpleDateFormat sf = new SimpleDateFormat("YY-MM-dd hh:mm:ss.SSS");
Date date1 = sf.parse(date1AsString);

boolean isBefore = date1.before(new Date());
boolean isAfter = date1.after(new Date());

Alternatively, you can compare the numeric values of the unix time to get the same result:

...

boolean isBefore = date1.getTime() < new Date().getTime();
boolean isAfter = date1.getTime() > new Date().getTime();
DTHENG
  • 198
  • 1
  • 7
-1

The Date Class has methods like before(date) and after(date). You should take a look into the Date Class.

Sal
  • 1,220
  • 13
  • 21
  • ok, but how does simpledateformat make the conversion to date? how can I make sure it is the same time? – Roxana Dec 16 '14 at 17:03
-1

So I think what you are looking for is to set the time zone of the SimpleDateFormat:

sf.setTimeZone(TimeZone.getTimeZone("EST"));
Aestel
  • 409
  • 8
  • 12