/** * 獲取指定時間戳所在日期的零點時間戳 * @param millis * @return */ public static long getDayZeroTimeInMillis(long millis){ Calendar c = Calendar.getInstance(); c.setTimeInMillis(millis); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return c.getTimeInMillis(); } @Test public void getDayZeroTimeInMillisTest() { System.out.println(System.currentTimeMillis());//1466388480064 當前系統時間 System.out.println(getDayZeroTimeInMillis(System.currentTimeMillis()));//1466352000000 當天凌晨時間點 } /** * 返回n天后/前的日期時間戳[零點時間戳] * @param days * 能夠爲任何整數,負數表示前N天,正數表示後N天 * @return TimeInMillis */ public static long getFutureInMillis(long current, int days) { current = current > 0 ? current : System.currentTimeMillis(); Calendar c = Calendar.getInstance(); c.setTimeInMillis(current); c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) + days); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return c.getTimeInMillis(); } @Test public void testFutureInMillis() throws Exception { System.out.println(getFutureInMillis(System.currentTimeMillis(), -1)); } /** * 將時間戳轉換爲格式化的字符串 * * @param format * @param millis * @return */ public static String getDateString(String format, long millis) { Date date = new Date(millis); if (isEmptyString(format)) format = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat dateFormat = new SimpleDateFormat(format); return dateFormat.format(date); } @Test public void testGetDateString() { System.out.println(getDateString("yyyy-MM-dd HH:mm:ss", 1466388294000L));//2016-06-20 10:04:54 }
mysteeljava
package com.mysteel.mobile.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import com.mysteel.mobile.entity.DesignOpRead; public class DateUtil { //遍歷時間段中全部的日期 public static List<Date> getAllDateInRange(Date startDate, Date endDate){ List<Date> dateList = new ArrayList<Date>(); if (startDate.before(endDate)){ Long spi = endDate.getTime() - startDate.getTime(); Long step = spi / (24 * 60 * 60 * 1000);// 相隔天數 dateList.add(endDate); for (int i = 1; i <= step; i++) { dateList.add(new Date(dateList.get(i - 1).getTime() - (24 * 60 * 60 * 1000)));// 比上一天減一 } } return dateList; } public static List<String> getAllDateStrInRange(String startDate, String endDate, String dateType) throws Exception { Long oneDay = 1000 * 60 * 60 * 24l; List<String> resultList = new ArrayList<String>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat yearSdf = new SimpleDateFormat("yyyy"); Date st = sdf.parse(startDate); Date et = sdf.parse(endDate); if (dateType.equals("week")) { long startTime = st.getTime(); long endTime = et.getTime(); while (startTime <= endTime + (7 * oneDay)) { Date date = new Date(startTime); DateTime jodaTime = new DateTime(date); int currWeek = jodaTime.getWeekOfWeekyear(); if (currWeek < 10) { resultList.add(jodaTime.getYear() + "0" + currWeek + ""); } else { resultList.add(jodaTime.getYear() + "" + currWeek + ""); } startTime = jodaTime.plusWeeks(1).getMillis(); } } else if (dateType.equals("month")) { long startTime = st.getTime(); long endTime = et.getTime(); int month = 0; while (startTime <= endTime) { Date date = new Date(startTime); DateTime jodaTime = new DateTime(date); // TODO int currMonth = jodaTime.getMonthOfYear(); if (month != currMonth) { if ((currMonth + 1) <= 10) { resultList.add(jodaTime.getYear() + "0" + (currMonth)); } else { resultList.add(jodaTime.getYear() + "" + (currMonth) + ""); } } startTime = jodaTime.plusMonths(1).getMillis(); } } else { long startTime = st.getTime(); long endTime = et.getTime(); while (startTime <= endTime) { Date d = new Date(startTime); //TODO YYYY-MM-DD 改 YYYYMMDD resultList.add(sdf2.format(d)); startTime += oneDay; } } return resultList; } /** * 把YYYYMMDD格式轉爲YYYY-MM-DD * @return * @throws ParseException */ public static String covertDay(String date) throws ParseException{ SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd"); Date day=sdf.parse(date); return sdf2.format(day); } public static List<String> getAllDateWithoutHoliday(String startDate, String endDate,List<String> holidayList,String dateType)throws Exception{ /*List<String> resultList = new ArrayList<String>(); SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd"); Date st = sdf.parse(startDate); Date et = sdf.parse(endDate); Long oneDay = 1000 * 60 * 60 * 24l; long startTime=st.getTime(); long endTime=et.getTime(); while(startTime<=endTime){ Date d = new Date(startTime); String dateStr=sdf2.format(d); if(!holidayList.contains(dateStr)){ resultList.add(dateStr); } startTime += oneDay; } return resultList;*/ Long oneDay = 1000 * 60 * 60 * 24l; List<String> resultList = new ArrayList<String>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat yearSdf = new SimpleDateFormat("yyyy"); Date st = sdf.parse(startDate); Date et = sdf.parse(endDate); if (dateType.equals("week")) { long startTime = st.getTime(); long endTime = et.getTime(); while (startTime <= endTime + (7 * oneDay)) { Date date = new Date(startTime); DateTime jodaTime = new DateTime(date); int currWeek = jodaTime.getWeekOfWeekyear(); if (currWeek < 10) { resultList.add(jodaTime.getYear() + "0" + currWeek + ""); } else { resultList.add(jodaTime.getYear() + "" + currWeek + ""); } startTime = jodaTime.plusWeeks(1).getMillis(); } } else if (dateType.equals("month")) { long startTime = st.getTime(); long endTime = et.getTime(); int month = 0; while (startTime <= endTime) { Date date = new Date(startTime); DateTime jodaTime = new DateTime(date); // TODO int currMonth = jodaTime.getMonthOfYear(); if (month != currMonth) { if ((currMonth + 1) <= 10) { resultList.add(jodaTime.getYear() + "0" + (currMonth)); } else { resultList.add(jodaTime.getYear() + "" + (currMonth) + ""); } } startTime = jodaTime.plusMonths(1).getMillis(); } } else { long startTime = st.getTime(); long endTime = et.getTime(); while (startTime <= endTime) { if(holidayList.contains(sdf.format(startTime))){ startTime += oneDay; continue; } Date d = new Date(startTime); //TODO YYYY-MM-DD 改 YYYYMMDD resultList.add(sdf2.format(d)); startTime += oneDay; } } return resultList; } public static List<String> getAllHourInDay(){ List<String> resultList = new ArrayList<String>(); for(int i=0;i<24;i++){ if(i<10){ resultList.add("0"+i+":00"); }else{ resultList.add(i+":00"); } } return resultList; } //新圖標X軸格式是這樣的00:00-01:00 偶數跳過 public static List<String> getNewAllHourInDay(){ List<String> resultList = new ArrayList<String>(); for(int i=0;i<24;i++){ String hour=""; //拼00:00-01:00 if(i+1!=10&&i<10){ hour="0"+i+":00-0"+(i+1)+":00"; }else if(i+1>=10){ hour=i+":00-"+(i+1)+":00"; } resultList.add(hour); } return resultList; } public static boolean isHoliday(String dateStr)throws Exception{ SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd"); Date date=sdf.parse(dateStr); Calendar cal = Calendar.getInstance(); cal.setTime(date); int day = cal.get(Calendar.DAY_OF_WEEK)-1; if(day==0||day==6){ return true; } return false; } public static List<String> getDataList()throws Exception{ //請注意月份是從0-11 Calendar start = Calendar.getInstance(); start.set(2016,0, 1); Calendar end = Calendar.getInstance(); end.set(2016, 11,30); int sumSunday = 0; int sumSat = 0; Set<String> dateSet = new TreeSet<String>(); SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); while(start.compareTo(end) <= 0) { int w = start.get(Calendar.DAY_OF_WEEK); if(w == Calendar.SUNDAY){ sumSunday ++; dateSet.add(format.format(start.getTime())); } if(w == Calendar.SATURDAY){ sumSat ++; dateSet.add(format.format(start.getTime())); } //打印天天 //System.out.println(format.format(start.getTime())); //循環,每次天數加1 start.set(Calendar.DATE, start.get(Calendar.DATE) + 1); } dateSet.add("20160101"); dateSet.add("20160207"); dateSet.add("20160208"); dateSet.add("20160209"); dateSet.add("20160210"); dateSet.add("20160211"); dateSet.add("20160212"); dateSet.add("20160213"); dateSet.add("20160404"); dateSet.add("20160501"); dateSet.add("20160502"); dateSet.add("20160609"); dateSet.add("20160610"); dateSet.add("20160611"); dateSet.add("20160915"); dateSet.add("20160916"); dateSet.add("20160917"); dateSet.add("20161001"); dateSet.add("20161002"); dateSet.add("20161003"); dateSet.add("20161004"); dateSet.add("20161005"); dateSet.add("20161006"); dateSet.add("20161007"); List<String> result = new ArrayList<String>(dateSet); return result; } // public static List<String> getWeekRange(List<String> rangeList){ List<String> weeks=new ArrayList<String>(); for(String weekDay:rangeList){ weeks.add(convertWeekToDate(weekDay)); } return weeks; } public static String convertMonth(String month) throws ParseException{ SimpleDateFormat sdf=new SimpleDateFormat("yyyyMM"); Date currMonth=sdf.parse(month); SimpleDateFormat monthSdf=new SimpleDateFormat("yyyy/MM"); return monthSdf.format(currMonth); } //傳入開始和結束日期,獲得一組月集合 格式爲yyyy/MM public static List<String> getMonthRange(String startDate,String endDate){ List<String> monthList=new ArrayList<String>(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat month=new SimpleDateFormat("yyyy/MM"); Date sDate; try { sDate = sdf.parse(startDate); Date eDate=sdf.parse(endDate); Calendar c = Calendar.getInstance(); Calendar d = Calendar.getInstance(); c.setTime(sDate); d.setTime(eDate); while(!c.after(d)){ //判斷月份 獲得月份 monthList.add(month.format(c.getTime())); c.add(Calendar.MONTH, 1); } } catch (ParseException e) { e.printStackTrace(); } return monthList; } public static List<DesignOpRead> getWeekRangeByDesignOpRead(List<DesignOpRead> rangeList){ List<DesignOpRead> weeks=new ArrayList<DesignOpRead>(); for(DesignOpRead weekDay:rangeList){ weekDay.setDate(convertWeekToDate(weekDay.getInweek())); } return weeks; } public static List<String> getMonthRange(List<DesignOpRead> rangeList){ List<String> monthList=new ArrayList<String>(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat month=new SimpleDateFormat("yyyy/MM"); for(DesignOpRead monthDay:rangeList){ monthDay.setDate(month.format(monthDay.getDate())); } return monthList; } //週轉日期 比方說2016年第一週就寫201601,獲得這周的第一天和最後一天 2016/12/26-2017/01/01 public static String convertWeekToDate(String weekDay){ DateTimeFormatter formatter= DateTimeFormat.forPattern("yyyyww"); DateTime d=DateTime.parse(weekDay,formatter); System.out.println(d.getWeekOfWeekyear()); String toDate=String.format( "%s-%s", d.dayOfWeek().withMinimumValue().minusDays(1) .toString("yyyy/MM/dd"), d.dayOfWeek().withMaximumValue().minusDays(1) .toString("yyyy/MM/dd")); return toDate; } public static void main(String args[]) throws Exception{ // String startDate = "20160101"; // String endDate ="20170801"; // List<String> range=getAllDateStrInRange(startDate, endDate, "week"); // for(String d:range){ // System.out.println(d); // } // String startDate = "201601"; // String endDate ="201701"; // // DateTimeFormatter formatter= DateTimeFormat.forPattern("yyyyee"); // DateTime d=DateTime.parse(startDate,formatter); // System.out.println(d.getWeekOfWeekyear()); // String a=String.format( // "%s-%s", // d.dayOfWeek().withMinimumValue() // .toString("yyyy/MM/dd"), // d.dayOfWeek().withMaximumValue() // .toString("yyyy/MM/dd")); // System.out.println(a); // System.out.println(convertWeekToDate("201644")); Map<String,Object> tree=new TreeMap<String,Object>(); tree.put("201605", new Object()); tree.put("201606", new Object()); tree.put("201607", new Object()); tree.put("201608", new Object()); tree.put("201609", new Object()); tree.put("201610", new Object()); Iterator<String> key=tree.keySet().iterator(); while(key.hasNext()){ System.out.println(key.next()); } } }