Java tutorial
//package com.java2s; //License from project: Apache License import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; public class Main { public static String getTimeWithFormat(Date date, String format) { if (format == null) { format = "yyyy-MM-dd HH:mm:ss"; } SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault()); if (date == null) { date = getDateTimeNow(); } return sdf.format(date); } public static Date getDateTimeNow() { return new Date(getCurrMillis()); } public static long getCurrMillis() { long _t = 0L; try { _t = Calendar.getInstance(Locale.getDefault()).getTimeInMillis(); return _t; } catch (IllegalArgumentException e) { e.printStackTrace(); } _t = new Date().getTime(); return _t; } }