package com.samsung.cloud.util; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; public class DateUtil { public static String YEAR = "year"; public static String WEEK = "week"; public static String DATEFORMATE = "yyyy/MM/dd"; public static String HYPHEN = "~"; /**get Date by String * * @param format :such as "yyyy-MM-dd HH:mm:ss","yyMMdd". * @return */ public static Date getDate(String source,String format) { Date date = null; try { DateFormat dateFormat = new SimpleDateFormat(format); //check the valid of date source //example:if source is "1996-13-3" ,it cannot be changed to "1997-1-3". dateFormat.setLenient(false); date = dateFormat.parse(source); } catch (ParseException e) { throw new RuntimeException("Invalid date: " + source); } return date; } /**get String by Date * * @param format :such as "yyyy-MM-dd HH:mm:ss","yyMMdd". * @return */ public static String getString(Date date,String format) { return new SimpleDateFormat(format).format(date); } /**get time by format ,such as "yyyy-MM-dd HH:mm:ss","yyMMdd". * * @param format * @return String with the format */ public static String getNow(String format){ String now = new SimpleDateFormat(format).format(new Date()); return now; } /**get tomorrow by format ,such as "yyyy-MM-dd HH:mm:ss","yyMMdd". * * @param * @return Date */ public static Date getTomorrow(){ Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_MONTH,1); return cal.getTime(); } /**get tomorrow by format ,such as "yyyy-MM-dd HH:mm:ss","yyMMdd". * * @param format * @return String with the format */ public static String getTomorrow(String format){ String tomorrow = new SimpleDateFormat(format).format(getTomorrow()); return tomorrow; } /**get tomorrow by format ,such as "yyyy-MM-dd HH:mm:ss","yyMMdd". * * @param Date date,format * @return String with the format */ public static String getTomorrow(Date date,String format){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DAY_OF_MONTH,1); String tomorrow = new SimpleDateFormat(format).format(cal.getTime()); return tomorrow; } /**get yesterday by format ,such as "yyyy-MM-dd HH:mm:ss","yyMMdd". * * @param * @return Date */ public static Date getYesterday(){ Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_MONTH,-1); return cal.getTime(); } /**get yesterday by format ,such as "yyyy-MM-dd HH:mm:ss","yyMMdd". * * @param String date,format * @return String with the format */ public static String getYesterday(Date date,String format){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DAY_OF_MONTH,-1); String yesterday = new SimpleDateFormat(format).format(cal.getTime()); return yesterday; } /**get yesterday by format ,such as "yyyy-MM-dd HH:mm:ss","yyMMdd". * * @param format * @return String with the format */ public static String getYesterday(String format){ String yesterday = new SimpleDateFormat(format).format(getYesterday()); return yesterday; } /**get dayOfWeek by Date * * @param * @return */ public static int getDayOfWeek(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.DAY_OF_WEEK); } /**get dayOfWeek * * @param format * @return */ public static int getDayOfWeek(String date,String format){ return getDayOfWeek(getDate(date,format)); } /**get dayOfYear by Date * * @param * @return */ public static int getDayOfYear(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.DAY_OF_YEAR); } /**get dayOfYear * * @param format * @return */ public static int getDayOfYear(String date,String format){ return getDayOfYear(getDate(date,format)); } /**get days By MonthOfYear * * @param * @return */ public static int getDaysByMonthOfYear(String monthOfYear,String format){ Calendar cal = Calendar.getInstance(); cal.setTime(getDate(monthOfYear,format)); return cal.getActualMaximum(Calendar.DAY_OF_MONTH); } /**get number of two days * * @param * @return */ public static int getDaysBetween(Date startDate, Date endDate){ Calendar cal = Calendar.getInstance(); cal.setTime(startDate); long startTime = cal.getTimeInMillis(); cal.setTime(endDate); long endTime = cal.getTimeInMillis(); long between_days = (endTime - startTime) / (1000 * 3600 * 24); return Integer.parseInt(String.valueOf(between_days)); } /**get number of two days * * @param format :such as "yyyy-MM-dd HH:mm:ss","yyMMdd". * @return */ public static int getDaysBetween(String startDate, String endDate, String format){ return getDaysBetween(getDate(startDate ,format),getDate( endDate ,format)); } /**compare two date String * * @param format :such as "yyyy-MM-dd HH:mm:ss","yyMMdd". * @return 1:date1 is after date2,0:equal,-1:date1 is before date2 */ public static int compareTwoDate(String date1,String date2,String format){ int flag = 0; if(!date1.equals(date2)){ Date d1 = getDate(date1 ,format); Date d2 = getDate(date2 ,format); flag = d1.compareTo(d2); } return flag; } /**get week of year by date * * @param * @return return value is a String like "yyyyWww". * @throws ParseException */ public static String getWeekOfYearByDay(Date date){ Calendar cl = Calendar.getInstance(); cl.setTime(date); int week = cl.get(Calendar.WEEK_OF_YEAR); cl.add(Calendar.DAY_OF_MONTH, -7); int year = cl.get(Calendar.YEAR); if(week<cl.get(Calendar.WEEK_OF_YEAR)){ year+=1; } return year + "W" + week; } /**get week of year by date * * @param format :such as "yyyy-MM-dd HH:mm:ss","yyMMdd". * @return return value is a String like "yyyyWww". */ public static String getWeekOfYearByDay(String date,String format) throws ParseException{ Date d = new Date(); d = getDate(date, format); return getWeekOfYearByDay(d); } /**get weeks of a year * * @param format :such as "yyyy-MM-dd HH:mm:ss","yyMMdd". * @return */ public static int getDaysByYear(String date,String format){ Calendar cal = Calendar.getInstance(); cal.setTime(getDate(date, format)); //get days number of the year cal.set(Calendar.MONTH,11); cal.set(Calendar.DAY_OF_MONTH,31); return cal.get(Calendar.DAY_OF_YEAR); } /**get weeks of a year * * @param format :such as "yyyy-MM-dd HH:mm:ss","yyMMdd". * @return */ public static int getWeeksByYear(String date,String format){ int weeks = 0; Calendar cal = Calendar.getInstance(); //get dayOfWeek on 01-01 cal.setTime(getDate(date, format)); cal.set(Calendar.MONTH,0); cal.set(Calendar.DAY_OF_MONTH,1); int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); //get days number of the year int yearDays = getDaysByYear(date, format); //calculate the weeks weeks = yearDays/7+1; int flag = yearDays % 7; if(flag > (7-dayOfWeek)){ weeks++; } return weeks; } /**get weeks of a year * * @param * @return */ public static int getWeeksByYear(int year){ String dateYear = String.valueOf(year); return getWeeksByYear(dateYear,"yyyy"); } /**get date by year,week,dayOfWeek * * @param year,week,dayOfWeek is the day in a week you want to get. * @return */ public static Date getDayByWeekOfYear(int year,int week,int dayOfWeek){ int maxWeeks = getWeeksByYear(year); if(week > maxWeeks){ throw new RuntimeException("The week is not valid!"); } if(1 > dayOfWeek || 7 < dayOfWeek ){ throw new RuntimeException("The dayOfWeek is not valid!"); } Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.WEEK_OF_YEAR, week); cal.set(Calendar.DAY_OF_WEEK, dayOfWeek); return cal.getTime(); } /**get year,week from String * * @param weekOfYear is a String like "yyyyWww",dayOfWeek is the day in a week you want to get. * @return */ public static Map<String,Integer> getYearAndWeekByString(String weekOfYear){ Map<String,Integer> map = new HashMap<String,Integer>(); if(!weekOfYear.matches("\d{4}W\d{2}")){ throw new RuntimeException("The format is not 'yyyyWww' !"); } int year = Integer.parseInt(weekOfYear.substring(0,4)); int week = Integer.parseInt(weekOfYear.substring(5,7)); map.put(YEAR,year); map.put(WEEK,week); return map; } /**get date by year,week,dayOfWeek * * @param weekOfYear is a String like "yyyyWww",dayOfWeek is the day in a week you want to get. * @return */ public static Date getDayByWeekOfYear(String weekOfYear,int dayOfWeek){ Map<String,Integer> map = getYearAndWeekByString(weekOfYear); int year = map.get(YEAR); int week = map.get(WEEK); return getDayByWeekOfYear(year,week,dayOfWeek); } /**get next week by week of year * * @param weekOfYear is a String like "yyyyWww",dayOfWeek is the day in a week you want to get. * @return */ public static String getNextWeekByWeekOfYear(String weekOfYear){ Map<String,Integer> map = getYearAndWeekByString(weekOfYear); int year = map.get(YEAR); int week = map.get(WEEK); //get the last day of the week Date lastDayOfWeek = getDayByWeekOfYear(year, week, 7); //get the first day of next week(next sunday) Calendar cal = Calendar.getInstance(); cal.setTime(lastDayOfWeek); cal.add(Calendar.WEEK_OF_YEAR,1); return getWeekOfYearByDay(cal.getTime()); } /**get last week by week of year * * @param weekOfYear is a String like "yyyyWww",dayOfWeek is the day in a week you want to get. * @return */ public static String getLastWeekByWeekOfYear(String weekOfYear){ Map<String,Integer> map = getYearAndWeekByString(weekOfYear); int year = map.get(YEAR); int week = map.get(WEEK); //get the first day of the week Date lastDayOfWeek = getDayByWeekOfYear(year, week, 1); Calendar cal = Calendar.getInstance(); cal.setTime(lastDayOfWeek); //get the last day of last week(last saturday) cal.add(Calendar.DAY_OF_MONTH,-1); return getWeekOfYearByDay(cal.getTime()); } public static void main(String[] args) { System.out.println("Today: " + getNow("yy-MM-dd HH:mm:ss")); System.out.println("Tomorrow: " + getTomorrow("yy-MM-dd HH:mm:ss")); System.out.println("Tomorrow: " + getTomorrow(getDate("2014-12-1","yy-MM-dd"),"yy-MM-dd")); System.out.println("Yesterday: " + getYesterday("yy-MM-dd HH:mm:ss")); System.out.println("Yesterday: " + getYesterday(getDate("2014-12-1","yy-MM-dd"),"yy-MM-dd")); System.out.println("Date formate: " + getDate("2013-04-08 23:12:34","yy-MM-dd HH:mm:ss")); System.out.println("String by Date: " + getString(getDate("2013-04-08 23:12:34","yy-MM-dd HH:mm:ss"),"yy/MM/dd HH:mm:ss")); System.out.println("Days between1: " + getDaysBetween(new Date(),getDate("2014-05-08 10:12:34","yy-MM-dd HH:mm:ss"))); System.out.println("Days between2: " + getDaysBetween("2013:04:19","2014:04:19","yy:MM:dd")); System.out.println("Days between2: " + getDaysBetween("2014-05-12","2014-05-06","yy-MM-dd")); System.out.println("Sunday OF Week: " + getDayByWeekOfYear(2000,54,5)); System.out.println("Sunday OF Week: " + getString(getDayByWeekOfYear("1914W20",1),"yy/MM/dd HH:mm:ss")); System.out.println("Compare two days: " + compareTwoDate("2014-05-19","2014-04-19","yy-MM-dd")); System.out.println("Weeks by year: " + getWeeksByYear("2000","yyyy")); System.out.println("Day of Week: " + getDayOfWeek("2000-1-2", "yy-MM-dd")); System.out.println("Day of Year: " + getDayOfYear("2000-1-2", "yy-MM-dd")); System.out.println("Days By Year: " + getDaysByYear("2000-1-2", "yy-MM-dd")); System.out.println("Days By MonthOfYear: " + getDaysByMonthOfYear("2001-2-2", "yy-MM-dd")); System.out.println("WeekOfYearByDay: " + getWeekOfYearByDay(getDate("2013-12-28","yy-MM-dd"))); System.out.println("WeekOfYearByDay: " + getWeekOfYearByDay(getDate("2014-1-1","yy-MM-dd"))); System.out.println("WeekOfYearByDay: " + getWeekOfYearByDay(getDate("2014-5-13","yy-MM-dd"))); System.out.println("WeekOfYearByDay: " + getWeekOfYearByDay(getDate("2014-12-31","yy-MM-dd"))); System.out.println("Next week: " + getNextWeekByWeekOfYear("2014W53")); System.out.println("Next week: " + getNextWeekByWeekOfYear("2000W54")); System.out.println("Weeks by year: " + getWeeksByYear(2000)); System.out.println("Last week: " + getLastWeekByWeekOfYear("2001W01")); System.out.println("Last week: " + getLastWeekByWeekOfYear("2013W53")); } }