Here you can find the source of getDayOffset(String date, int offset, String format)
Parameter | Description |
---|---|
date | a parameter |
offset | a parameter |
format | a parameter |
public static String getDayOffset(String date, int offset, String format)
//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); } } }