Here you can find the source of getDateBeforeTheDay(String day, int num)
public static String getDateBeforeTheDay(String day, int num)
//package com.java2s; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Main { public static final String YYYYMMDD = "yyyy-MM-dd"; private static final long MILLISECOND_IN_DAY = 1000 * 60 * 60 * 24; public static String getDateBeforeTheDay(String day, int num) { long longtimes = convertStrToDate(day).getTime() - num * MILLISECOND_IN_DAY; Date date = new Date(longtimes); return convertDateToStr(date, YYYYMMDD); }//from www . jav a2 s . c o m public static Date convertStrToDate(String s) { try { DateFormat dateformat = DateFormat.getDateInstance(); Date date = dateformat.parse(s); return date; } catch (Exception exception) { exception.printStackTrace(); Calendar cal = Calendar.getInstance(); cal.set(1900, 0, 1); return cal.getTime(); } } public static Date convertStrToDate(String s, String format) { SimpleDateFormat simpledateformat = new SimpleDateFormat(format); try { Date date = simpledateformat.parse(s); return date; } catch (Exception exception) { exception.printStackTrace(); Calendar cal = Calendar.getInstance(); cal.set(1900, 0, 1); return cal.getTime(); } } public static String convertDateToStr(Date d, String format) { SimpleDateFormat simpledateformat = new SimpleDateFormat(format); String s; try { s = simpledateformat.format(d).toString(); return s; } catch (Exception e) { s = "1900-01-01"; } return s; } }