Java Date Before getDateBeforeTheDay(String day, int num)

Here you can find the source of getDateBeforeTheDay(String day, int num)

Description

get Date Before The Day

License

Open Source License

Declaration

public static String getDateBeforeTheDay(String day, int num) 

Method Source Code


//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;
    }
}

Related

  1. getDateBeforeHours(Date date, int hours)
  2. getDateBeforeNextMonth(Date date, Integer month, Integer day)
  3. getDateBeforeOrAfter(int iDate)
  4. getDateBeforeOrAfterV2(int idx)
  5. getDateBeforeSomeMinutes(int minute, long timestamp)
  6. getDateBeforeToday(int num)
  7. getDateBeforTwelveMonth()
  8. getDateTimeAfterOfBeforeHour(Integer AfterOfBeforeHour)
  9. getDayBeforeOrAfter2(Date time, int days)