Java tutorial
//package com.java2s; //License from project: LGPL import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class Main { public static String previosTimeDate(int sub) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); java.util.Calendar calendar = java.util.Calendar.getInstance(); java.util.Date date = null; try { //date = simpleDateFormat.parse(simpleDateFormat.format(new Date())); java.util.Date currentTime = calendar.getTime(); calendar.setTime(currentTime); // add(Calendar.DAY_OF_MONTH, -5). calendar.add(java.util.Calendar.HOUR_OF_DAY, -sub); } catch (Exception e) { e.printStackTrace(); } return simpleDateFormat.format(calendar.getTime()); } public static String getTime() { return getCurrentDateTime("yyyy-MM-dd HH:mm:ss"); } public static String getCurrentDateTime() { Date today = new Date(); Locale currentLocale = new Locale("KOREAN", "KOREA"); // String pattern = "yyyyMMddHHmmss"; String pattern = "yyyy-MM-dd HH:mm:ss"; // String pattern = "yyyy-MM-dd"; SimpleDateFormat formatter = new SimpleDateFormat(pattern, currentLocale); return formatter.format(today); } public static String getCurrentDateTime(String pattern) { SimpleDateFormat simpleDateFormat = (pattern == null) ? new SimpleDateFormat() : new SimpleDateFormat(pattern); return simpleDateFormat.format(new Date()); } }