Java tutorial
//package com.java2s; //License from project: Apache License import java.util.Date; import java.util.GregorianCalendar; public class Main { public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss"; public static String timeFormat(long date, String format) { GregorianCalendar gc = new GregorianCalendar(); gc.setTime(new Date(date)); java.text.SimpleDateFormat fm = null; if (format != null) { fm = new java.text.SimpleDateFormat(format); } else { fm = new java.text.SimpleDateFormat(DEFAULT_FORMAT); } return fm.format(gc.getTime()); } public static String timeFormat(Date date, String format) { GregorianCalendar gc = new GregorianCalendar(); gc.setTime(date); java.text.SimpleDateFormat fm = null; if (format != null) { fm = new java.text.SimpleDateFormat(format); } else { fm = new java.text.SimpleDateFormat(DEFAULT_FORMAT); } return fm.format(gc.getTime()); } }