views:

229

answers:

2

I have some java code that parses a string and creates a Date object. On Linux, everything works fine, but on Windows it continuously starts at 19:00:00 rather than 00:00:00. Here is the code:

if(currTask != null) {
   if((m = p0.matcher(currTask)).matches()) {
    date = new Date(Long.valueOf(m.group(2)) - Long.valueOf(m.group(1)));
   }
   else if((m = p.matcher(currTask)).matches()) {
    date = new Date(System.currentTimeMillis() - Long.valueOf(m.group(1)));
   }

   return padded(date.getHours())+":"+padded(date.getMinutes())+":"+padded(date.getSeconds());
  }

The returned value is the problem on Windows. Is this some inconsistency with how one of Date's methods works on Windows as opposed to Linux? Thanks for your help.

  • Ken
+5  A: 

Check that your time zone is the same on both platforms... my guess is that they're not. (Print out TimeZone.getDefault().getDisplayName() to see what the default is.)

However, you shouldn't be using Date.getHours()/getMinutes()/getSeconds()/getSeconds() anyway; they're deprecated. You could use Calendar... or you could bite the bullet and use Joda Time, which is a far superior date and time API.

What does your input look like and what's the result meant to be?

Jon Skeet
Yep. Most of Date's methods are deprecated, so I would not rely on them.
Brandon Bodnár
+2  A: 

The difference must be in your locale. There are environment variables that affect this on Linux. Let me guess: you're in EST (GMT-5)?

wrang-wrang