Java Compare dates

  • Thread starter Mangalaganesh Balasubramanian
  • Start date
M

Mangalaganesh Balasubramanian

Hi,

I have a date (only hours:minutes:seconds) in a text file against
which i need to compare the current time.

The date in the text file is expressed in GMT.

How do i compare and compute the difference (i am only interested in
the hours,minutes and seconds)?

The only way looks like, creating a new Date/Calendar object from the
hh:mm:ss and setting the day, month and year from a new instance of
Date/Calendar and then computing the difference.

This could run into issues when we are in the next day while GMT is
still "yesterday" during which this computation would fail.

Are there other ways to do that?

Appreciate any assistance.

Thanks,
Manglu
 
A

Andrew Thompson

Mangalaganesh Balasubramanian wrote:
...
I have a date (only hours:minutes:seconds) in a text file against
which i need to compare the current time.

If the time difference between whatever tool is writing
that file, and your app. is unknown, ..
The date in the text file is expressed in GMT.

How do i compare and compute the difference (i am only interested in
the hours,minutes and seconds)?

..this would seem to be an incomputable problem.

OTOH If the time difference can be obtained. ..

The only way looks like, creating a new Date/Calendar object from the
hh:mm:ss and setting the day, month and year from a new instance of
Date/Calendar and then computing the difference.

This could run into issues when we are in the next day while GMT is
still "yesterday" during which this computation would fail.

.. Do as you were desribing, but subtract (the known time
difference from current time).

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
 
E

edgar

Hi,

I have a date (only hours:minutes:seconds) in a text file against
which i need to compare the current time.

The date in the text file is expressed in GMT.

How do i compare and compute the difference (i am only interested in
the hours,minutes and seconds)?

The only way looks like, creating a new Date/Calendar object from the
hh:mm:ss and setting the day, month and year from a new instance of
Date/Calendar and then computing the difference.

This could run into issues when we are in the next day while GMT is
still "yesterday" during which this computation would fail.

Are there other ways to do that?

Appreciate any assistance.

Thanks,
Manglu


I assume you'll be comparing the time in your file to the current
time, which is the system time. So I don't think you'll have a need to
create a Date/Calendar object. You might want to look into
java.lang.System
 
L

Lew

Hi,

I have a date (only hours:minutes:seconds) in a text file against
which i need to compare the current time.

The date in the text file is expressed in GMT.

How do i compare and compute the difference (i am only interested in
the hours,minutes and seconds)?

The only way looks like, creating a new Date/Calendar object from the
hh:mm:ss and setting the day, month and year from a new instance of
Date/Calendar and then computing the difference.
[/QUOTE]

Yes, that's a convenient and easy way to do what you're asking.

That doesn't make sense. If you compare GMT (really, UTC) times, then you are
getting a valid comparison. You've eliminated time zone issues. What are you
referring to?
I assume you'll be comparing the time in your file to the current
time, which is the system time. So I don't think you'll have a need to
create a Date/Calendar object. You might want to look into
java.lang.System

Why the aversion to creating a Calendar or Date object?

How does System help? What's wrong with:

Calendar calNow = Calendar.getInstance();
Calendar calStored = Calendar.getInstance();
calStored.set( yearFromFile(), monthFromFile(), dayFromFile(),
hourFromFile(), minFromFile(), secFromFile() );
// DateFormat would help here
long diffInMs = calNow.getTimeInMillis() - calStored.getTimeInMillis();
etc.?
 
C

Chris

Mangalaganesh said:
Hi,

I have a date (only hours:minutes:seconds) in a text file against
which i need to compare the current time.

The date in the text file is expressed in GMT.

How do i compare and compute the difference (i am only interested in
the hours,minutes and seconds)?

The only way looks like, creating a new Date/Calendar object from the
hh:mm:ss and setting the day, month and year from a new instance of
Date/Calendar and then computing the difference.

This could run into issues when we are in the next day while GMT is
still "yesterday" during which this computation would fail.

Are there other ways to do that?

I'm not sure the best approach for your particular app, but if you can
avoid using Dates and Calendars and the like, you're better off (usually).

Think about converting to millis (System.currentTimeMillis) or doing
string or int comparisons. Calendars only make sense when you have to do
complicated date manipulations which can't easily be done with primitives.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top