Long Time-stamp to Date Conversion in Java - Java
Home » Java

Long Time-stamp to Date Conversion in Java

10 February 2010 No Comment

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);

	}

}

 

More Related Posts in Java

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.