package com.espeed.yxy.tool; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 程序名称: EspeedMail_时速邮箱 * 程序版本: V1.0 * 作 者: 深圳市科飞时速网络技术有限公司(0755-88843776) * 版权所有: 深圳市科飞时速网络技术有限公司 * 技术支持: Tech@21gmail.com * 单元名称: 日期转换工具类(营销游) * 开始时间: 2013.12.09 * 程 序 员: 谢勇 * 最后修改: * 备 注: 如需修改请通知程序员 */ public class DateFormat { private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); private static SimpleDateFormat sdf2 = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); /**将字符串转换日期*/ public static Date dateToString(String str) { Date dateNum = null; try { dateNum = sdf.parse(str); } catch (ParseException e) { e.printStackTrace(); } return dateNum; } /** 将字符串转换日期*/ public static Date dateToString2(String str) { Date dateNum = null; try { dateNum = sdf2.parse(str); } catch (ParseException e) { e.printStackTrace(); } return dateNum; } /** 将日期转换为字符串*/ public static String simpleDataFormat(Date date) {//yy必须小写,MM是月份,mm是分;HH是24小时制,而hh是12小时制*/ String str = sdf.format(date); return str; } /**给定一个日期型字符串,返回加减n天后的日期型字符串*/ public static String nDaysAfterOneDateString(String basicDate, int n) { Date tmpDate = null; try { tmpDate = sdf2.parse(basicDate); System.out.println(tmpDate); } catch (Exception e) { //日期型字符串格式错误 } long nDay = (tmpDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n)* (24 * 60 * 60 * 1000); tmpDate.setTime(nDay); return sdf2.format(tmpDate); } /**给定一个日期,返回加减n天后的日期*/ public static Date nDaysAfterOneDate(Date basicDate, int n) { long nDay = (basicDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n) * (24 * 60 * 60 * 1000); basicDate.setTime(nDay); return basicDate; } /**判断日期为星期几,0为星期天,依此类推*/ public static String dayOfWeek(String strdate) { SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日"); Date date = null; try { date = df.parse(strdate); } catch (ParseException e) { System.out.println("字符型转日期有异常"); } //首先定义一个calendar,必须使用getInstance()进行实例化 Calendar aCalendar = Calendar.getInstance(); //里面野可以直接插入date类型 aCalendar.setTime(date); //计算此日期是一周中的哪一天 int x = aCalendar.get(Calendar.DAY_OF_WEEK) - 1; String week = ""; switch (x) { case 0: week = "星期天"; break; case 1: week = "星期一"; break; case 2: week = "星期二"; break; case 3: week = "星期三"; break; case 4: week = "星期四"; break; case 5: week = "星期五"; break; case 6: week = "星期六"; break; } return week; } /**判断日期为星期几,0为星期天,依此类推*/ public static int dayOfWeekNum(String strdate) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = df.parse(strdate); } catch (ParseException e) { System.out.println("字符型转日期有异常"); } //首先定义一个calendar,必须使用getInstance()进行实例化 Calendar aCalendar = Calendar.getInstance(); //里面野可以直接插入date类型 aCalendar.setTime(date); //计算此日期是一周中的哪一天 int x = aCalendar.get(Calendar.DAY_OF_WEEK) - 1; int week = 0; switch (x) { case 0: week = 7; break; case 1: week = 1; break; case 2: week = 2; break; case 3: week = 3; break; case 4: week = 4; break; case 5: week = 5; break; case 6: week = 6; break; } return week; } /** 计算两个日期相隔的天数*/ public static int nDaysBetweenTwoDate(Date firstDate, Date secondDate) { int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000)); return nDay; } /**计算两个日期相隔的天数*/ public static int nDaysBetweenTwoDate(String firstString, String secondString) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date firstDate = null; Date secondDate = null; try { firstDate = df.parse(firstString); secondDate = df.parse(secondString); } catch (Exception e) { //日期型字符串格式错误 } int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000)); System.out.println("相差"+nDay+"天"); return nDay; } /** 某年某月有多少天*/ public static int yearMonthDay(int year, int month) { int day = 0; if (((month >= 1) && (month <= 12)) && (year >= 0)) { if (month == 2) { if ((((year % 4) == 0) && ((year % 100) != 0))|| ((year % 400) == 0)) day = 29; else day = 28; } else { if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) day = 31; else day = 30; } System.out.println(year + "年" + month + "月的天数为:" + day); } else { System.out.println("请输入正确的年月!"); } return day; } /**判断两时间相差的分钟数*/ public static long minute1s(String beginDate,String endDate){ Date begin = null; Date end = null; SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long minute1=0; try { begin = dfs.parse(beginDate); end = dfs.parse(endDate); long between = (end.getTime() - begin.getTime()) / 1000;//除以1000是为了转换成秒 minute1 = between / 60; } catch (Exception e) { e.printStackTrace(); } return minute1; } /**判断两时间相差的分钟数*/ public static boolean isthisTimer(String nowTimer){ int hour=Integer.parseInt(nowTimer.split(":")[0]); int minute=Integer.parseInt(nowTimer.split(":")[1]); System.out.println(nowTimer); System.out.println(hour); System.out.println(minute); if(hour==23||hour==0){ if(hour==23&&minute>=30||hour==0&&minute<=30){ return true; }else{ return false; } }else{ return false; } } //获取当前时间加减后的日期 public static String lastDays(int n){ String resultdate=""; try { //获取几天之前的时间 SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd"); //字符串转换 Calendar c = Calendar.getInstance(); c.setTimeInMillis(new Date().getTime()); c.add(Calendar.DATE, n);//多少天后的日期 Date date= new Date(c.getTimeInMillis()); //将c转换成Date resultdate=formatDate.format(date); } catch (Exception e) { resultdate=""; e.printStackTrace(); } return resultdate; } public static void main(String[] args) { try { SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); String nowdate=df.format(new Date()); System.out.println(minute1s("2016-10-10 10:10:00","2016-10-10 10:20:00")); } catch (Exception e) { e.printStackTrace(); } } }