Java Week Day getPreviousOrNextDay(String time, int dayNum)

Here you can find the source of getPreviousOrNextDay(String time, int dayNum)

Description

get Previous Or Next Day

License

Apache License

Declaration

public static String getPreviousOrNextDay(String time, int dayNum) throws ParseException 

Method Source Code


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

Related

  1. getNextDayOfweek()
  2. getPreDay()
  3. getPreviousDay(long date, int startOfWeek)
  4. getPreviousFriday(String date)
  5. getPreviousMonday(Date date, int days)
  6. getPreWeekLastDay()
  7. getShortWeekday(int dayOfWeek)
  8. getShortWeekday(String day)
  9. getSQLForWeekSunday(String weekString)