Jumat, 09 Mei 2014

Long Time-stamp to Date Conversion in Java

I wanted to convert a timestamp from long value to something human understandable date. I explored multiple ways of doing it with a couple of controlling parameters. Sharing it as it might be useful to you.

To present the output, there are two alternatives, one is going for manual business and other is using date formatter. Though first one is manual work, it is discussed here because you may require a format that is not readily available in standard formats provided by Java. Sometimes the time output is expected in a predefined timezone. We may want to convert the time in required timezone. Example of this is also shown here.

I am not going through the code details as these are self explanatory.



import java.text.SimpleDateFormat;


import java.util.Calendar;


import java.util.Date;


import java.util.GregorianCalendar;


import java.util.TimeZone;


public class TimestampConvertor {


public static void main(String[] args) {


long input = 1265110426299L;


TimestampConvertor tsc = new TimestampConvertor();


//Get output in system timezone


System.out.println("usingDateAndCalendar in local timezone: " + tsc.usingDateAndCalendar(input));


//Get output in GMT


System.out.println("usingDateAndCalendarWithTimeZone: " + tsc.usingDateAndCalendarWithTimeZone(input));


//Get output in system timezone


System.out.println("usingDateFormatter in local timezone: " + tsc.usingDateFormatter(input));


//Get output in GMT


System.out.println("usingDateFormatterWithTimeZone: " + tsc.usingDateFormatterWithTimeZone(input));


}


private String usingDateAndCalendar(long input){


Date date = new Date(input);


Calendar cal = new GregorianCalendar();


cal.setTime(date);


return(cal.get(Calendar.YEAR) 


+ "/" + cal.get(Calendar.MONTH)


+ "/" + cal.get(Calendar.DATE)


+ " " + cal.get(Calendar.HOUR)


+ ":" + cal.get(Calendar.MINUTE)


+ ":" + cal.get(Calendar.SECOND)


+ (cal.get(Calendar.AM_PM)==0?"AM":"PM")


);


}


private String usingDateAndCalendarWithTimeZone(long input){


Date date = new Date(input);


Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));


cal.setTime(date);


return(cal.get(Calendar.YEAR) 


+ "/" + cal.get(Calendar.MONTH)


+ "/" + cal.get(Calendar.DATE)


+ " " + cal.get(Calendar.HOUR)


+ ":" + cal.get(Calendar.MINUTE)


+ ":" + cal.get(Calendar.SECOND)


+ (cal.get(Calendar.AM_PM)==0?"AM":"PM")


);


}


private String usingDateFormatter(long input){


Date date = new Date(input);


Calendar cal = new GregorianCalendar();


SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MMM/dd hh:mm:ss z");


sdf.setCalendar(cal);


cal.setTime(date);


return sdf.format(date);


}


private String usingDateFormatterWithTimeZone(long input){


Date date = new Date(input);


Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));


SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MMM/dd hh:mm:ss z");


sdf.setCalendar(cal);


cal.setTime(date);


return sdf.format(date);


}


}



Source:http://www.tutorialsdesk.com/2012/08/long-time-stamp-to-date-conversion-in.html

Tidak ada komentar:

Posting Komentar