|
@@ -0,0 +1,763 @@
|
|
|
+package com.jiayue.ipfcst.common.core.util;
|
|
|
+
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.ParsePosition;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by bqw on 14/11/19.
|
|
|
+ */
|
|
|
+public class TimeUtils {
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 今日凌晨 毫秒
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static long getCurrentTime(){
|
|
|
+
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期的增加月数
|
|
|
+ * add by xxl
|
|
|
+ * @param date 日期
|
|
|
+ * @param addMonth 增加的月数,正数表示增加,负数表示减少
|
|
|
+ * @return Date 日期的增加月数后的结果
|
|
|
+ */
|
|
|
+ public static Date addMonth(Date date, int addMonth) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.MONTH, addMonth);
|
|
|
+ return new Date(calendar.getTime().getTime());
|
|
|
+ }
|
|
|
+ public static Date addYear(Date date, int addYear) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.MONTH, addYear);
|
|
|
+ return new Date(calendar.getTime().getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期的增加天数
|
|
|
+ * add by xxl
|
|
|
+ * @param date 日期
|
|
|
+ * @param addDay 增加的天数,正数表示增加,负数表示减少
|
|
|
+ * @return Date 日期的增加月数后的结果
|
|
|
+ */
|
|
|
+ public static Date addDay(Date date, int addDay) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, addDay);
|
|
|
+ return new Date(calendar.getTime().getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取输入日期所属月份第一天
|
|
|
+ * add by xxl
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getFirstDateMonth(Date date) {
|
|
|
+
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Date newDate = null;
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ int start = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, start);
|
|
|
+ newDate = new Date(calendar.getTime().getTime());
|
|
|
+ return newDate;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取输入日期所属月份最后一天
|
|
|
+ * add by xxl
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getLastDayOfMonth(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Date newDate = null;
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ int end = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, end);
|
|
|
+ newDate = new Date(calendar.getTime().getTime());
|
|
|
+ return newDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * creat by linwb 20160908
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+
|
|
|
+ public static Date getFirstDateYear(Date date){
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Date newDate = null;
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ int start = calendar.getMinimum(Calendar.DAY_OF_YEAR);
|
|
|
+ calendar.set(Calendar.DAY_OF_YEAR,start);
|
|
|
+ newDate = new Date(calendar.getTime().getTime());
|
|
|
+ return newDate;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public static Date getLastDayOfyear(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Date newDate = null;
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ int end = calendar.getActualMaximum(Calendar.DAY_OF_YEAR);
|
|
|
+ calendar.set(Calendar.DAY_OF_YEAR, end);
|
|
|
+ newDate = new Date(calendar.getTime().getTime());
|
|
|
+ return newDate;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到本季度第一天的日期
|
|
|
+ * @Methods Name getFirstDayOfQuarter
|
|
|
+ * @return Date
|
|
|
+ */
|
|
|
+ public static Date getFirstDayOfQuarter(Date date) {
|
|
|
+ Calendar cDay = Calendar.getInstance();
|
|
|
+ cDay.setTime(date);
|
|
|
+ int curMonth = cDay.get(Calendar.MONTH);
|
|
|
+ if (curMonth >= Calendar.JANUARY && curMonth <= Calendar.MARCH){
|
|
|
+ cDay.set(Calendar.MONTH, Calendar.JANUARY);
|
|
|
+ }
|
|
|
+ if (curMonth >= Calendar.APRIL && curMonth <= Calendar.JUNE){
|
|
|
+ cDay.set(Calendar.MONTH, Calendar.APRIL);
|
|
|
+ }
|
|
|
+ if (curMonth >= Calendar.JULY && curMonth <= Calendar.AUGUST) {
|
|
|
+ cDay.set(Calendar.MONTH, Calendar.JULY);
|
|
|
+ }
|
|
|
+ if (curMonth >= Calendar.OCTOBER && curMonth <= Calendar.DECEMBER) {
|
|
|
+ cDay.set(Calendar.MONTH, Calendar.OCTOBER);
|
|
|
+ }
|
|
|
+ cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMinimum(Calendar.DAY_OF_MONTH));
|
|
|
+ System.out.println(cDay.getTime());
|
|
|
+ return cDay.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到本季度最后一天的日期
|
|
|
+ * @Methods Name getLastDayOfQuarter
|
|
|
+ * @return Date
|
|
|
+ */
|
|
|
+ public static Date getLastDayOfQuarter(Date date) {
|
|
|
+ Calendar cDay = Calendar.getInstance();
|
|
|
+ cDay.setTime(date);
|
|
|
+ int curMonth = cDay.get(Calendar.MONTH);
|
|
|
+ if (curMonth >= Calendar.JANUARY && curMonth <= Calendar.MARCH){
|
|
|
+ cDay.set(Calendar.MONTH, Calendar.MARCH);
|
|
|
+ }
|
|
|
+ if (curMonth >= Calendar.APRIL && curMonth <= Calendar.JUNE){
|
|
|
+ cDay.set(Calendar.MONTH, Calendar.JUNE);
|
|
|
+ }
|
|
|
+ if (curMonth >= Calendar.JULY && curMonth <= Calendar.AUGUST) {
|
|
|
+ cDay.set(Calendar.MONTH, Calendar.AUGUST);
|
|
|
+ }
|
|
|
+ if (curMonth >= Calendar.OCTOBER && curMonth <= Calendar.DECEMBER) {
|
|
|
+ cDay.set(Calendar.MONTH, Calendar.DECEMBER);
|
|
|
+ }
|
|
|
+ cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
+ return cDay.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception{
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(new Date());
|
|
|
+ System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
|
|
|
+
|
|
|
+// Date date = addMonth(new Date(),1);
|
|
|
+// System.out.println(DateFormatUtils.format(date.getTime(), "yyyyMM"));
|
|
|
+
|
|
|
+// CronExpression cronExpression =new CronExpression("0 0 18 9 * ?");
|
|
|
+// String str = cronExpression.getExpressionSummary();
|
|
|
+// String crons = "{"+str.replaceAll("\n",",")+"}";
|
|
|
+// JSONObject json = JSONUtil.parseObj(crons);
|
|
|
+// String daysOfMonth = json.get("daysOfMonth").toString();
|
|
|
+// System.out.println(daysOfMonth);
|
|
|
+// System.out.println(cronExpression.getExpressionSummary());
|
|
|
+// System.out.println(cronExpression.getFinalFireTime());
|
|
|
+
|
|
|
+
|
|
|
+// long ll=1467188298610l;
|
|
|
+// NumberFormat nf=NumberFormat.getInstance();
|
|
|
+// nf.setGroupingUsed(false);
|
|
|
+// System.out.println(""+nf.format(ll));
|
|
|
+//
|
|
|
+// String dd="1467188298610333";
|
|
|
+// BigDecimal bd=new BigDecimal(dd);
|
|
|
+// double dds=bd.doubleValue();
|
|
|
+// NumberFormat nf1 =NumberFormat.getInstance();
|
|
|
+// nf1.setGroupingUsed(false);
|
|
|
+// System.out.println(nf.format(dds));
|
|
|
+//
|
|
|
+// System.out.println(new Double("1467188298610333"));
|
|
|
+// String date = "2017-06-01";
|
|
|
+// getDateWithOutZero(date,"-");
|
|
|
+// String date = DateFormatUtils.format(Long.valueOf("1496214861000"), "yyyy/MM/dd");
|
|
|
+// String timed = date;
|
|
|
+//
|
|
|
+// String date10 = date.substring(5, 7);
|
|
|
+// if(date10.compareTo("10") < 0)timed = date.substring(0,5) + date.substring(6, date.length());
|
|
|
+// else timed = date;
|
|
|
+//
|
|
|
+// if(timed.lastIndexOf("-") > 0){
|
|
|
+// int pos = timed.lastIndexOf("-");
|
|
|
+// String day = timed.substring(pos + 1,pos + 3);
|
|
|
+// if(day.compareTo("10") < 0)timed = timed.substring(0,pos + 1) + timed.substring(pos + 2, timed.length());
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// System.out.println(timed.toString());
|
|
|
+
|
|
|
+// String time10 = timel.substring(0, 2);
|
|
|
+// if(time10.compareTo("10") < 0)time10 = time10.substring(1,2);
|
|
|
+//
|
|
|
+// System.out.println(timel);
|
|
|
+// System.out.println(time10);
|
|
|
+// Calendar cal = Calendar.getInstance();
|
|
|
+// int current = 0;
|
|
|
+// int suff = 0;
|
|
|
+// long time = System.currentTimeMillis();
|
|
|
+// cal.setTimeInMillis(Long.valueOf("1456120500000"));
|
|
|
+// int currentMinute = cal.get(Calendar.MINUTE);
|
|
|
+// current = currentMinute % 15;
|
|
|
+// suff = currentMinute / 15;
|
|
|
+// if (current < 4) {
|
|
|
+// // 存储period的整数值
|
|
|
+// cal.set(Calendar.MINUTE, suff * 15);
|
|
|
+// // 赋值
|
|
|
+// time = cal.getTimeInMillis();
|
|
|
+// }
|
|
|
+// System.out.println(DateFormatUtils.format(time, "yyyy-MM-dd HH:mm:ss"));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取int型的4位年和月
|
|
|
+ * 参数:
|
|
|
+ * 返回: Map<String,Integer>
|
|
|
+ * 创建时间:2016-11-07
|
|
|
+ * 作者: chendi
|
|
|
+ */
|
|
|
+ public static Map<String,Integer> getCurrenYearAndMonth(){
|
|
|
+ Map<String,Integer> result = new HashMap();
|
|
|
+ Date d = new Date();
|
|
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
|
|
|
+ String year = simpleDateFormat.format(d);
|
|
|
+ simpleDateFormat.applyPattern("MM");
|
|
|
+ String month = simpleDateFormat.format(d);
|
|
|
+ result.put("year", Integer.parseInt(year));
|
|
|
+ result.put("month", Integer.parseInt(month));
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取一个月的开始时间和结束时间(1日零点、最后一天23点59分59秒)
|
|
|
+ * 参数:year month 传入年月则按照该年月计算起始,传入空则取当前时间接
|
|
|
+ * 返回: Map<String,Long> key:"startTime" "endTime"
|
|
|
+ * 创建时间:2016-11-07
|
|
|
+ * 作者: chendi
|
|
|
+ */
|
|
|
+ public static Map<String,Long> getCurrentMonthStartTimeAndEndTime(){
|
|
|
+ return getMonthStartTimeAndEndTime(null,null);
|
|
|
+ }
|
|
|
+ public static Map<String,Long> getMonthStartTimeAndEndTime(Integer year,Integer month){
|
|
|
+ Map<String,Long> result = new HashMap();
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ if(year!=null){
|
|
|
+ cal.set(Calendar.YEAR,year);
|
|
|
+ }
|
|
|
+ if(month!=null){
|
|
|
+ cal.set(Calendar.MONTH,month - 1);
|
|
|
+ }
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY,0);
|
|
|
+ cal.set(Calendar.MINUTE,0);
|
|
|
+ cal.set(Calendar.SECOND,0);
|
|
|
+ Date firstDay = getFirstDateMonth(cal.getTime());
|
|
|
+ Date nextMonth = TimeUtils.addMonth(firstDay, 1);
|
|
|
+ nextMonth = getFirstDateMonth(nextMonth);
|
|
|
+ result.put("startTime",firstDay.getTime());
|
|
|
+ result.put("endTime",nextMonth.getTime()-1*1000);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断时间是否在时间段内
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * 当前时间 yyyy-MM-dd HH:mm:ss
|
|
|
+ * @param strDateBegin
|
|
|
+ * 开始时间 00:00:00
|
|
|
+ * @param strDateEnd
|
|
|
+ * 结束时间 00:05:00
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isInDate(Date date, String strDateBegin,
|
|
|
+ String strDateEnd) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String strDate = sdf.format(date);
|
|
|
+ //System.out.println(strDate);
|
|
|
+ // 截取当前时间时分秒
|
|
|
+ int strDateH = Integer.parseInt(strDate.substring(11, 13));
|
|
|
+ int strDateM = Integer.parseInt(strDate.substring(14, 16));
|
|
|
+ int strDateS = Integer.parseInt(strDate.substring(17, 19));
|
|
|
+ //System.out.println("strDateH:"+strDateH+"|strDateM:"+strDateM+"|strDateS:"+strDateS);
|
|
|
+ // 截取开始时间时分秒
|
|
|
+ int strDateBeginH = Integer.parseInt(strDateBegin.substring(11, 13));
|
|
|
+ int strDateBeginM = Integer.parseInt(strDateBegin.substring(14, 16));
|
|
|
+ int strDateBeginS = Integer.parseInt(strDateBegin.substring(17, 19));
|
|
|
+ //System.out.println("strDateBeginH:"+strDateBeginH+"|strDateBeginM:"+strDateBeginM+"|strDateBeginS:"+strDateBeginS);
|
|
|
+
|
|
|
+ // 截取结束时间时分秒
|
|
|
+ int strDateEndH = Integer.parseInt(strDateEnd.substring(11, 13));
|
|
|
+ int strDateEndM = Integer.parseInt(strDateEnd.substring(14, 16));
|
|
|
+ int strDateEndS = Integer.parseInt(strDateEnd.substring(17, 19));
|
|
|
+ //System.out.println("strDateEndH:"+strDateEndH+"|strDateEndM:"+strDateEndM+"|strDateEndS:"+strDateEndS);
|
|
|
+
|
|
|
+ if ((strDateH >= strDateBeginH && strDateH <= strDateEndH)) {
|
|
|
+ //System.out.println("strDateEndH1:");
|
|
|
+
|
|
|
+ // 当前时间小时数在开始时间和结束时间小时数之间
|
|
|
+ if (strDateH > strDateBeginH && strDateH < strDateEndH) {
|
|
|
+
|
|
|
+ return true;
|
|
|
+ // 当前时间小时数等于开始时间小时数,分钟数在开始和结束之间
|
|
|
+ } else if (strDateH == strDateBeginH && strDateM >= strDateBeginM
|
|
|
+ && strDateM <= strDateEndM) {
|
|
|
+ //System.out.println("strDateEndH2:");
|
|
|
+
|
|
|
+ return true;
|
|
|
+ // 当前时间小时数等于开始时间小时数,分钟数等于开始时间分钟数,秒数在开始和结束之间
|
|
|
+ } else if (strDateH == strDateBeginH && strDateM == strDateBeginM
|
|
|
+ && strDateS >= strDateBeginS && strDateS <= strDateEndS) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ // 当前时间小时数大等于开始时间小时数,等于结束时间小时数,分钟数小等于结束时间分钟数
|
|
|
+ else if (strDateH >= strDateBeginH && strDateH == strDateEndH
|
|
|
+ && strDateM <= strDateEndM) {
|
|
|
+ return true;
|
|
|
+ // 当前时间小时数大等于开始时间小时数,等于结束时间小时数,分钟数等于结束时间分钟数,秒数小等于结束时间秒数
|
|
|
+ } else return strDateH >= strDateBeginH && strDateH == strDateEndH
|
|
|
+ && strDateM == strDateEndM && strDateS <= strDateEndS;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据type获取两日期差
|
|
|
+ * @param start 开始时间
|
|
|
+ * @param end 结束时间
|
|
|
+ * @param type days表示相差天数 hours小时数 minutes分钟数
|
|
|
+ * @return 返回int,未设置type则返回0
|
|
|
+ */
|
|
|
+ public static int getDateDiffer(Date start,Date end,String type){
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ String startDate = sdf.format(start);
|
|
|
+ String endDate = sdf.format(end);
|
|
|
+ try {
|
|
|
+ long from = sdf.parse(startDate).getTime();
|
|
|
+ long to = sdf.parse(endDate).getTime();
|
|
|
+
|
|
|
+ if (type.equals("days")){ // 返回相差天数
|
|
|
+ int days = (int) ((to - from)/(1000 * 60 * 60 * 24));
|
|
|
+ return days;
|
|
|
+ } else if (type.equals("hours")){ // 返回相差小时数
|
|
|
+ int hours = (int) ((to - from)/(1000 * 60 * 60));
|
|
|
+ return hours;
|
|
|
+ } else if (type.equals("minutes")){ // 返回相差分钟数
|
|
|
+ int minutes = (int) ((to - from)/(1000 * 60));
|
|
|
+ return minutes;
|
|
|
+ }
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改时间为不包含0的数据,格式必须报分隔符
|
|
|
+ * @Title: getDateWithOutZero
|
|
|
+ * @Description: TODO
|
|
|
+ * @param date
|
|
|
+ */
|
|
|
+ public static String getDateWithOutZero(String date,String split) {
|
|
|
+ String timed = date;
|
|
|
+ String date10 = date.substring(5, 7);
|
|
|
+ if(date10.compareTo("10") < 0)timed = date.substring(0,5) + date.substring(6, date.length());
|
|
|
+ else timed = date;
|
|
|
+
|
|
|
+ if(timed.lastIndexOf(split) > 0){
|
|
|
+ int pos = timed.lastIndexOf(split);
|
|
|
+ String day = timed.substring(pos + 1,pos + 3);
|
|
|
+ if(day.compareTo("10") < 0)timed = timed.substring(0,pos + 1) + timed.substring(pos + 2, timed.length());
|
|
|
+ }
|
|
|
+ System.out.println(timed);
|
|
|
+ return timed;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前时间的后一天时间
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ */
|
|
|
+ public static Date getAfterDay(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, +1);
|
|
|
+ date = calendar.getTime();
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取时间的前一天时间
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ */
|
|
|
+ public static Date getBeforeDay(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, -1);
|
|
|
+ date = calendar.getTime();
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// ----------------------------------------------------------------------------------------------------------------
|
|
|
+// bizy添加时间工具
|
|
|
+ /**
|
|
|
+ * 时间戳转日期(传过来的参数没除1000)
|
|
|
+ * @param ms
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String transForDateShort(Long ms){
|
|
|
+ String str = "";
|
|
|
+ if(ms!=null){
|
|
|
+ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ if(ms!=null){
|
|
|
+ try {
|
|
|
+ str=sdf.format(ms);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间戳转日期(传过来的参数没除1000)
|
|
|
+ * @param ms
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String transForDate(Long ms){
|
|
|
+ String str = "";
|
|
|
+ if(ms!=null){
|
|
|
+ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ if(ms!=null){
|
|
|
+ try {
|
|
|
+ str=sdf.format(ms);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取现在时间
|
|
|
+ *
|
|
|
+ * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static Date getNowDate() {
|
|
|
+ Date currentTime = new Date();
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String dateString = formatter.format(currentTime);
|
|
|
+ ParsePosition pos = new ParsePosition(0);
|
|
|
+ Date currentTime_2 = formatter.parse(dateString, pos);
|
|
|
+ return currentTime_2;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取现在时间
|
|
|
+ *
|
|
|
+ * @return返回短时间格式 yyyy-MM-dd
|
|
|
+ */
|
|
|
+ public static Date getNowDateShort() {
|
|
|
+ Date currentTime = new Date();
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String dateString = formatter.format(currentTime);
|
|
|
+ ParsePosition pos = new ParsePosition(0);
|
|
|
+ Date currentTime_2 = formatter.parse(dateString, pos);
|
|
|
+ return currentTime_2;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取现在时间
|
|
|
+ *
|
|
|
+ * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getStringDate() {
|
|
|
+ Date currentTime = new Date();
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String dateString = formatter.format(currentTime);
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取现在时间
|
|
|
+ *
|
|
|
+ * @return 返回短时间字符串格式yyyy-MM-dd
|
|
|
+ */
|
|
|
+ public static String getStringDateShort() {
|
|
|
+ Date currentTime = new Date();
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String dateString = formatter.format(currentTime);
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当日凌晨时间
|
|
|
+ *
|
|
|
+ * @return 返回短时间字符串格式yyyy-MM-dd
|
|
|
+ */
|
|
|
+ public static String getStringDateShortLc(Date currentTime) {
|
|
|
+// Date currentTime = new Date();
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
|
|
|
+ String dateString = formatter.format(currentTime);
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取现在时间
|
|
|
+ *
|
|
|
+ * @return 返回短时间字符串格式yyyy-MM-dd
|
|
|
+ */
|
|
|
+ public static String getStringDateShortYM() {
|
|
|
+ Date currentTime = new Date();
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
|
|
|
+ String dateString = formatter.format(currentTime);
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取时间 小时:分;秒 HH:mm:ss
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getTimeShort() {
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("HHmmss");
|
|
|
+ Date currentTime = new Date();
|
|
|
+ String dateString = formatter.format(currentTime);
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
|
|
|
+ *
|
|
|
+ * @param strDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date strToDateLong(String strDate) {
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ ParsePosition pos = new ParsePosition(0);
|
|
|
+ Date strtodate = formatter.parse(strDate, pos);
|
|
|
+ return strtodate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
|
|
|
+ *
|
|
|
+ * @param strDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date strToDateLongHM(String strDate) {
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
|
|
|
+ ParsePosition pos = new ParsePosition(0);
|
|
|
+ Date strtodate = formatter.parse(strDate, pos);
|
|
|
+ return strtodate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
|
|
|
+ *
|
|
|
+ * @param dateDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String dateToStrLong(Date dateDate) {
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String dateString = formatter.format(dateDate);
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将短时间格式时间转换为字符串 yyyy-MM-dd
|
|
|
+ *
|
|
|
+ * @param dateDate
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String dateToStr(Date dateDate) {
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String dateString = formatter.format(dateDate);
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将短时间格式字符串转换为时间 yyyy-MM-dd
|
|
|
+ *
|
|
|
+ * @param strDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date strToDate(String strDate) {
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ ParsePosition pos = new ParsePosition(0);
|
|
|
+ Date strtodate = formatter.parse(strDate, pos);
|
|
|
+ return strtodate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到现在时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getNow() {
|
|
|
+ Date currentTime = new Date();
|
|
|
+ return currentTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 提取一个月中的最后一天
|
|
|
+ *
|
|
|
+ * @param day
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getLastDate(long day) {
|
|
|
+ Date date = new Date();
|
|
|
+ long date_3_hm = date.getTime() - 3600000 * 34 * day;
|
|
|
+ Date date_3_hm_date = new Date(date_3_hm);
|
|
|
+ return date_3_hm_date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到现在时间
|
|
|
+ *
|
|
|
+ * @return 字符串 yyyyMMdd HHmmss
|
|
|
+ */
|
|
|
+ public static String getStringToday() {
|
|
|
+ Date currentTime = new Date();
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ String dateString = formatter.format(currentTime);
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到现在月份
|
|
|
+ *
|
|
|
+ * @return 字符串 yyyyMMdd HHmmss
|
|
|
+ */
|
|
|
+ public static String getStringTodayMonth() {
|
|
|
+ Date currentTime = new Date();
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ String dateString = formatter.format(currentTime);
|
|
|
+ String month;
|
|
|
+ month = dateString.substring(4, 6);
|
|
|
+ return month;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到现在小时
|
|
|
+ */
|
|
|
+ public static String getHour() {
|
|
|
+ Date currentTime = new Date();
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String dateString = formatter.format(currentTime);
|
|
|
+ String hour;
|
|
|
+ hour = dateString.substring(11, 13);
|
|
|
+ return hour;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到现在分钟
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getTime() {
|
|
|
+ Date currentTime = new Date();
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String dateString = formatter.format(currentTime);
|
|
|
+ String min;
|
|
|
+ min = dateString.substring(14, 16);
|
|
|
+ return min;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
|
|
|
+ *
|
|
|
+ * @param sformat yyyyMMddhhmmss
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getUserDate(String sformat) {
|
|
|
+ Date currentTime = new Date();
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(sformat);
|
|
|
+ String dateString = formatter.format(currentTime);
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得指定日期的前一天
|
|
|
+ * @param specifiedDay
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String getSpecifiedDayBefore(String specifiedDay){
|
|
|
+//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ Date date=null;
|
|
|
+ try {
|
|
|
+ date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ c.setTime(date);
|
|
|
+ int day=c.get(Calendar.DATE);
|
|
|
+ c.set(Calendar.DATE,day-1);
|
|
|
+
|
|
|
+ String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
|
|
|
+ return dayBefore;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|