Here you can find the source of getPreviousDateTimeString(String format)
public static String getPreviousDateTimeString(String format)
//package com.java2s; import java.util.Calendar; import java.sql.Timestamp; import java.text.SimpleDateFormat; public class Main { public static String getPreviousDateTimeString(int days) { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_MONTH, -days); return toDateTimeString(calendar); }/* ww w .ja v a2 s . co m*/ public static String getPreviousDateTimeString(String datetime, int days) { Calendar calendar = toCalendar(datetime); calendar.add(Calendar.DAY_OF_MONTH, -days); return toDateTimeString(calendar); } public static String getPreviousDateTimeString(String format) { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_MONTH, -1); return toDateTimeString(calendar, format); } public static String getPreviousDateTimeString() { return getPreviousDateTimeString("yyyy-MM-dd HH:mm:ss"); } public static String toDateTimeString(java.util.Date date) { if (date == null) return ""; String dateString = toDateString(date); String timeString = toTimeString(date); if (dateString == null || timeString == null) return ""; return dateString + " " + timeString; } public static String toDateTimeString(Calendar calendar) { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()); } public static String toDateTimeString(Calendar calendar, String format) { return toDateTimeString(calendar.getTime(), format); } public static String toDateTimeString(long datetime) { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(datetime)); } public static String toDateTimeString(long datetime, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(new java.util.Date(datetime)); } public static String toDateTimeString(java.util.Date date, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } public static String toDateTimeString(Timestamp time) { long datetime = time.getTime(); return toDateTimeString(datetime); } public static String toDateTimeString(java.sql.Date date) { long datetime = date.getTime(); return toDateTimeString(datetime); } public static Calendar toCalendar(String yearStr, String monthStr, String dayStr, String hourStr, String minuteStr, String secondStr) { int year, month, day, hour, minute, second; try { year = Integer.parseInt(yearStr); month = Integer.parseInt(monthStr); day = Integer.parseInt(dayStr); hour = Integer.parseInt(hourStr); minute = Integer.parseInt(minuteStr); second = Integer.parseInt(secondStr); } catch (Exception e) { return null; } return toCalendar(year, month, day, hour, minute, second); } public static Calendar toCalendar(String datetime) { int index = datetime.indexOf(" "); String date = datetime.substring(0, index); String time = datetime.substring(index + 1); int dateSlash1 = date.indexOf("-"); int dateSlash2 = date.lastIndexOf("-"); if (dateSlash1 <= 0 || dateSlash1 == dateSlash2) return null; int timeColon1 = time.indexOf(":"); int timeColon2 = time.lastIndexOf(":"); if (timeColon1 <= 0 || timeColon1 == timeColon2) return null; String yearStr = date.substring(0, dateSlash1); String monthStr = date.substring(dateSlash1 + 1, dateSlash2); String dayStr = date.substring(dateSlash2 + 1); String hourStr = time.substring(0, timeColon1); String minuteStr = time.substring(timeColon1 + 1, timeColon2); String secondStr = time.substring(timeColon2 + 1); ; int year, month, day, hour, minute, second; try { year = Integer.parseInt(yearStr); month = Integer.parseInt(monthStr); day = Integer.parseInt(dayStr); hour = Integer.parseInt(hourStr); minute = Integer.parseInt(minuteStr); second = Integer.parseInt(secondStr); Calendar calendar = Calendar.getInstance(); calendar.set(year, month - 1, day, hour, minute, second); return calendar; } catch (Exception e) { return null; } } public static Calendar toCalendar(int[] dates) { if (dates == null || dates.length < 6) return null; return toCalendar(dates[0], dates[1], dates[2], dates[3], dates[4], dates[5]); } public static Calendar toCalendar(int year, int month, int day, int hour, int minute, int second) { Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month - 1); c.set(Calendar.DATE, day); c.set(Calendar.HOUR_OF_DAY, hour); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, second); return c; } public static String toDateString(java.util.Date date, String spe) { if (date == null) return ""; Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); int year = calendar.get(Calendar.YEAR); String monthStr = "" + month; String dayStr = "" + day; String yearStr = "" + year; if (month < 10) monthStr = "0" + month; if (day < 10) dayStr = "0" + day; return yearStr + spe + monthStr + spe + dayStr; } public static String toDateString(java.util.Date date) { return toDateString(date, "-"); } public static String toDateString(Calendar calendar) { return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()); } public static String toTimeString(java.util.Date date) { if (date == null) return ""; Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return toTimeString(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)); } public static String toTimeString(int hour, int minute, int second) { String hourStr = "" + hour; String minuteStr = "" + minute; String secondStr = "" + second; if (hour < 10) hourStr = "0" + hour; if (minute < 10) minuteStr = "0" + minute; if (second < 10) secondStr = "0" + second; return hourStr + ":" + minuteStr + ":" + secondStr; } }