Here you can find the source of getPreviousOrNextDay(String time, int dayNum)
public static String getPreviousOrNextDay(String time, int dayNum) throws ParseException
//package com.java2s; //License from project: Apache License import java.text.ParseException; import java.text.SimpleDateFormat; 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 final String DEFAULT_FORMAT_NOHOUR = "yyyy-MM-dd"; public static String getPreviousOrNextDay(String time, int dayNum) throws ParseException { SimpleDateFormat df = new SimpleDateFormat(DEFAULT_FORMAT_NOHOUR); Date date = df.parse(time); GregorianCalendar currentDate = new GregorianCalendar(); currentDate.setTime(date);//from ww w . j a va 2s. c o m currentDate.add(GregorianCalendar.DATE, dayNum); Date monday = currentDate.getTime(); String preMonday = df.format(monday); return preMonday; } public static long getTime(String dateStr, String formate) { Date date; try { date = getDate(dateStr, formate); return date.getTime(); } catch (ParseException e) { return -1; } } public static long getTime(String dateStr) { return getTime(dateStr, DEFAULT_FORMAT); } public static String getDate(Date date, String formate) { SimpleDateFormat sf = new SimpleDateFormat(formate); return sf.format(date); } public static Date getDate(String dateStr, String formate) throws ParseException { SimpleDateFormat sf = new SimpleDateFormat(formate); return sf.parse(dateStr); } }