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

}

3 Responses to Long Time-stamp to Date Conversion in Java

  1. Manoja says:

    hi

    how to convert an unix timestamp into normal human readable date format..??

    Actually i’m having log file with fields like timestamp,userid,url,etc… In this file my first field is unix timestamp(1299004147.855) this i have to convert.. by using language-java. actually i did program by giving input..bt i need to convert it in the file itself so plz can u tell me how to do..??

    thank you

  2. Neha says:

    thanks !!! it was really helpful

  3. Yoomi says:

    Thank you so much!!!
    It was really helpful!!!

Leave a Reply

Your email address will not be published. Required fields are marked *


6 − three =

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

Powered by WordPress | Designed by: seo service | Thanks to seo company, web designers and internet marketing company