0

I have a long value that represents the time of day in milliseconds since midnight that day. i.e. 00:00:01 would be 1000.

I want to convert this to a long timestamp since the epoch - using the current System's day, month, year. What's the best way to do this?

James
  • 1,237
  • 4
  • 22
  • 43
  • Which day? Which data type are you interested in? It's really not clear what you're asking. Also, bear in mind time zones - do you have a "time of day" in a UTC day, or in some specific time zone? – Jon Skeet Jan 22 '13 at 09:50
  • The current system day and date, sorry should have specified – James Jan 22 '13 at 09:52

2 Answers2

1
    final Calendar instance = Calendar.getInstance();
    instance.set(Calendar.HOUR, 0);
    instance.set(Calendar.MINUTE, 0);
    instance.set(Calendar.SECOND, 0);
    instance.set(Calendar.MILLISECOND, 0);
    long result = instance.getTimeInMillis() + yourTime;

Also note that Calendar#getInstance does the following:

Calendar cal = createCalendar(TimeZone.getDefaultRef(), Locale.getDefault());

The resulting time will change according to the default set TimeZone (and yes it can change !). See this post: java Timezone setDefault effects

Community
  • 1
  • 1
Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
0

Try this:

Date date = new Date(your_long_value);
BackSlash
  • 21,927
  • 22
  • 96
  • 136