package com.gds.web.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateOpUtil { /** * @param args * @throws ParseException */ public static void main(String[] args) throws Exception { System.out.println(daysBetween("2015-09-28 10:10:10","2015-10-01 23:00:00")); } /* *日期格式转换 yyyy-MM-dd */ public static String turnDateFormat(String date)throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.format(format.parse(date)); } /* * 两日期比较时间 */ public static boolean dateEqual(String begin_date,String end_date)throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date d1 = format.parse(begin_date); Date d2 = format.parse(end_date); if(d1.getTime() == d2.getTime()) { return true ; } return false; } /* * 两日期比较时间 */ public static boolean dateDef(String now_date,String use_date)throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date now_time = format.parse(now_date); Date end_time = format.parse(use_date); if(end_time.getTime() >= now_time.getTime()) { return true ; } return false; } /** * 计算两日期相隔天数 *字符串的日期格式的计算 */ public static int daysBetween(String smdate,String bdate) throws Exception{ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(smdate)); long time1 = cal.getTimeInMillis(); cal.setTime(sdf.parse(bdate)); long time2 = cal.getTimeInMillis(); long between_days=(time2-time1)/(1000*3600*24); return Integer.parseInt(String.valueOf(between_days)); } /** * 获得指定日期的后一天 * * @param specifiedDay * @return * @throws Exception */ public static String getSpecifiedDayBefore(String specifiedDay,int i) {//可以用new Date().toLocalString()传递参数 Calendar c = Calendar.getInstance(); Date date = null; try { date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay); } catch (ParseException e) { e.printStackTrace(); } c.setTime(date); int day = c.get(Calendar.DATE); c.set(Calendar.DATE, day + i); String dayAfter = new SimpleDateFormat("yyyy-MM-dd") .format(c.getTime()); return dayAfter; } /** * 判断当前日期是星期几 * * @param pTime 修要判断的时间 * @return dayForWeek 判断结果 * @Exception 发生异常 */ public static int dayForWeek(String pTime) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.setTime(format.parse(pTime)); int dayForWeek = 0; if(c.get(Calendar.DAY_OF_WEEK) == 1){ dayForWeek = 7; }else{ dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1; } return dayForWeek; } }
package com.gds.web.util; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import org.apache.commons.lang3.StringUtils; import com.ctc.wstx.util.StringUtil; /** * @author microe * * */ public class FixedDate { /** * 获得指定日期的前一天 * * @param specifiedDay * @return * @throws Exception */ public static String getSpecifiedDayBefore(String specifiedDay,int i) {//可以用new Date().toLocalString()传递参数 Calendar c = Calendar.getInstance(); Date date = null; try { date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay); } catch (ParseException e) { e.printStackTrace(); } c.setTime(date); int day = c.get(Calendar.DATE); c.set(Calendar.DATE, day - i); String dayAfter = new SimpleDateFormat("yyyy-MM-dd") .format(c.getTime()); return dayAfter; } /* * 获取指定日期,本周的周一日期 */ public static String getMondayOfThisWeek(String fixed_date) throws Exception { DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd"); Date date = fmt.parse(fixed_date); Calendar c = Calendar.getInstance(); c.setTime(date); int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1; if (day_of_week == 0) day_of_week = 7; c.add(Calendar.DATE, -day_of_week + 1); return fmt.format(c.getTime())+ " 00:00:00"; } /* * 获取指定日期,本月的一号日期 */ public static String getDateOfThisMonth(String fixed_date) throws Exception { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); //获取当前月第一天: Date date1 = fmt.parse(fixed_date); Calendar c = Calendar.getInstance(); c.setTime(date1); c.add(Calendar.MONTH, 0); c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 return fmt.format(c.getTime())+ " 00:00:00"; } /** * 当前季度的开始时间,即2012-01-1 00:00:00 * * @return */ public static String getCurrentQuarterStartTime(String fixed_date) throws Exception { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = fmt.parse(fixed_date); Calendar c = Calendar.getInstance(); c.setTime(date1); int currentMonth = c.get(Calendar.MONTH) + 1; if (currentMonth >= 1 && currentMonth <= 3) c.set(Calendar.MONTH, 0); else if (currentMonth >= 4 && currentMonth <= 6) c.set(Calendar.MONTH, 3); else if (currentMonth >= 7 && currentMonth <= 9) c.set(Calendar.MONTH, 4); else if (currentMonth >= 10 && currentMonth <= 12) c.set(Calendar.MONTH, 9); c.set(Calendar.DATE, 1); return fmt.format(c.getTime()) + " 00:00:00"; } //获取上半年、下半年开始时间 public static String getHalfYearStartTime(String fixed_date) throws Exception { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = fmt.parse(fixed_date); Calendar c = Calendar.getInstance(); c.setTime(date1); int currentMonth = c.get(Calendar.MONTH) + 1; String date = null; try { if (currentMonth >= 1 && currentMonth <= 6){ c.set(Calendar.MONTH, 0); }else if (currentMonth >= 7 && currentMonth <= 12){ c.set(Calendar.MONTH, 6); } c.set(Calendar.DATE, 1); date = fmt.format(c.getTime())+ " 00:00:00"; } catch (Exception e) { e.printStackTrace(); } return date; } /** * 当前年的开始时间,即2012-01-01 00:00:00 * * @return */ public static String getCurrentYearStartTime(String fixed_date) throws Exception { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = fmt.parse(fixed_date); Calendar c = Calendar.getInstance(); c.setTime(date1); c.set(Calendar.MONTH, 0); c.set(Calendar.DATE, 1); return fmt.format(c.getTime())+ " 00:00:00"; } public static void main(String[] args) throws Exception { System.out.println( getCurrentQuarterStartTime("2015-09-09") ); } }
时间: 2024-10-12 08:17:59