Java Day getDayOffset(String date, int offset, String format)

Here you can find the source of getDayOffset(String date, int offset, String format)

Description

get Day Offset

License

Apache License

Parameter

Parameter Description
date a parameter
offset a parameter
format a parameter

Declaration

public static String getDayOffset(String date, int offset, String format) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;

import java.util.Locale;

public class Main {
    /**/*from w ww. j av a 2s .  c o  m*/
     * 
     * @param offset
     * @param format
     * @return
     */
    public static String getDayOffset(int offset, String format) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + offset);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(calendar.getTime());
    }

    /**
     * 
     * @param date
     * @param offset
     * @param format
     * @return
     */
    public static String getDayOffset(String date, int offset, String format) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(toDate(date));
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + offset);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(calendar.getTime());
    }

    /**
     * 
     * @param date
     * @return
     */
    public static Date toDate(String date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (date.length() == 10)
                sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.parse(date);
        } catch (ParseException pe) {
            throw new RuntimeException(pe);
        }
    }

    /**
     * 
     * @param date
     * @param format
     * @return
     */
    public static Date toDate(String date, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(date);
        } catch (ParseException pe) {
            throw new RuntimeException(pe);
        }
    }
}

Related

  1. getDayName(String dateString)
  2. getDayNameForDate(java.util.Date dt, boolean fullname)
  3. getDayNames(Locale locale)
  4. getDayNumber()
  5. getDayOfDate(Date date)
  6. getDayofTheDate(Date d1)
  7. getDAYOFWEEK(String strDate)
  8. getDayRange(String bday, String eday)
  9. getDays(Date day, int preDays, String format)