Reputation: 3117
String mytime = "5177706";
long epoch = Long.parseLong( mytime );
Date expiry = new Date( epoch * 1000 );
When I convert epoch time to date, the result is
Mon Mar 02 05:45:06 SGT 1970
Cause of epoch time, the year is 1970, although the day and month of the answer is desired result.
How can I convert the year to current year as a output eg. Mon Mar 02 05:45:06 SGT 2012 ?
Upvotes: 1
Views: 3666
Reputation: 6665
The following code adds the epoch
, 5177706 seconds, to January 1st of the current year, which is January 1st, 2012 00:00:00:000 GMT.
public static void main(String[] args) {
String mytime = "5177706";
long epoch = Long.parseLong(mytime);
// Get January 1st of the current year in GMT.
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
// Convert epoch to milliseconds and add to January 1st of the current year.
Date expiry = new Date(calendar.getTime().getTime() + epoch * 1000);
// Output the expiry date in Asia/Singapore (SGT) time.
DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
format.setTimeZone(TimeZone.getTimeZone("Asia/Singapore"));
System.out.println(format.format(expiry));
}
The above code outputs Thu Mar 01 06:15:06 SGT 2012
. The month, day, and time differ from the expected Mar 02 05:45:06 SGT
for the following reasons:
Singapore Standard Time (SGT) changed from UTC+07:30 to UTC+08:00 on December 31st 1981, so add 30 minutes to the time:
Mar 02 05:45:06 SGT
+ 30 minutes = Mar 02 06:15:06 SGT
.
2012 is a leap year, so subtract a day:
Mar 02 06:15:06 SGT
- 1 day = Mar 01 06:15:06 SGT
Upvotes: 0
Reputation: 2804
Your question isn't very clear (sounds like some weird HW problem). Anyway, you just have to add the time elapsed since the date you provided (in milliseconds).
msOriginal
is the date you provided in ms.msElapsed
is total ms elapsed since 1970. This is equal to 2012 - 1970 = 42
years.msElapsedLeapYears
is the total leapyear days elapsed. If you look at the wiki article, List of leap years you can see that there were 11 years. So you have to account for 11 days.The sum of the above 3 fields gives you the result you are looking for. Notice, the dates don't fall on the same day (March 1st is not a Sunday in 2012). If you'd like the day to be the same, it needs to be 2015 (45 years). Basically, the number of days elapsed mod 7 needs to be 0 for the date to fall on the same day.
This sample code is done in groovy so the syntax is slightly different from regular java.
long epoch = 5177706l;
Calendar current = Calendar.getInstance();
current.setTimeInMillis(epoch*1000);
println current.getTime();
long msOriginal = (epoch*1000);
long msElapsed = 42l*365*24l*3600*1000;
long msElapsedLeapYears = 11l*24*3600*1000;
current.setTimeInMillis(msOriginal+msElapsed+msElapsedLeapYears);
println current.getTime();
Result:
Sun Mar 01 16:15:06 CST 1970
Thu Mar 01 16:15:06 CST 2012
Upvotes: 0
Reputation: 47964
Basically you just want to do this:
Calendar cal = Calendar.getInstance();
cal.setTime(expiry);
cal.set(Calendar.YEAR, 2012);
Date expiryThisYear = cal.getTime();
Raises some interesting questions about how you came to have 517706 as an input in the first place, but the world is a strange place sometimes :)
Upvotes: 1