[Q#8237193][A#24182811] How to convert currentTimeMillis to a date in Java?
https://stackoverflow.com/q/8237193
I have milliseconds in certain log file generated in server, I also know the locale from where the log file was generated, my problem is to convert milliseconds to date in specified format. The processing of that log is happening on server located in different time zone. While converting to "SimpleDateFormat" program is taking date of the machine as such formatted date do not represent correct time of the server. Is there any way to handle this elegantly ? Output:
Answer
https://stackoverflow.com/a/24182811
APIzation
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeStamp);
int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
package com.stackoverflow.api;
import java.util.Calendar;
public class Human24182811 {
public static int[] fromMillsToDate(long timeStamp) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeStamp);
int out[] = new int[3];
out[0] = calendar.get(Calendar.YEAR);
out[1] = calendar.get(Calendar.MONTH);
out[2] = calendar.get(Calendar.DAY_OF_MONTH);
return out;
}
}
package com.stackoverflow.api;
import java.util.Calendar;
/**
* How to convert currentTimeMillis to a date in Java?
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/24182811">https://stackoverflow.com/a/24182811</a>
*/
public class APIzator24182811 {
public static int convertCurrenttimemillis(int timeStamp)
throws Exception {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeStamp);
int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH);
return calendar.get(Calendar.DAY_OF_MONTH);
}
}