Skip to content
Advertisement

How do you create a proper Epoch calendar object in Java?

I want to create a calendar object that is the epoch date. What’s the ‘correct’ (if any) way to do it?

Date epochDate = new java.text.SimpleDateFormat ("dd/MM/yyyy HH:mm:ss z").parse("01/01/1970 01:00:00 GMT");
Calendar epochCal;
epochCal.setTime(epochDate);

// or maybe

Calendar epochCal;
epochCal.setTimeInMillis(1);

Answer

The ‘setTimeInMillis()’ method would work fine and be easily understood by others. It might also be clearer if you passed it 0 instead of 1. The first method has more line noise and just adds more chances that something can get screwed up in maintenance.

Advertisement