package cn.com.dekn.common.util; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import java.util.Date; public class DateUtil { public static Timestamp getNowTime() { return new Timestamp(System.currentTimeMillis()); } public static String getYear() { SimpleDateFormat date = new SimpleDateFormat("yy"); return date.format(new Date()); } public static String getYearYYYY() { SimpleDateFormat date = new SimpleDateFormat("yyyy"); return date.format(new Date()); } public static String getMonth() { SimpleDateFormat date = new SimpleDateFormat("MM"); return date.format(new Date()); } public static String getDay() { SimpleDateFormat date = new SimpleDateFormat("dd"); return date.format(new Date()); } public static String getHour() { SimpleDateFormat date = new SimpleDateFormat("HH"); return date.format(new Date()); } public static String getMinute() { SimpleDateFormat date = new SimpleDateFormat("mm"); return date.format(new Date()); } public static String getSecond() { SimpleDateFormat date = new SimpleDateFormat("ss"); return date.format(new Date()); } public static String getMillisecond() { SimpleDateFormat date = new SimpleDateFormat("SSS"); return date.format(new Date()); } public static boolean isToday(Timestamp time) { SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd"); if (format.format(time).equals(format.format(new Date()))) { return true; } return false; } public static String getCurrentDay(String dateFormat) { SimpleDateFormat format = new SimpleDateFormat(dateFormat); return format.format(new Date()); } public static long getDayBetween(String now, String fore) { long count = 0L; SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd"); try { Date nowDate = myFormatter.parse(now); Date foreDate = myFormatter.parse(fore); count = (nowDate.getTime() - foreDate.getTime()) / 86400000L; } catch (ParseException e) { count = 0L; e.printStackTrace(); } return count; } public static Timestamp getTodayTimestamp() { String nowDateStr = getCurrentDay("yyyy-MM-dd"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Timestamp timestamp = null; try { timestamp = new Timestamp(sdf.parse(nowDateStr).getTime()); } catch (ParseException e) { e.printStackTrace(); } return timestamp; } public static Timestamp rollDayTimestamp(String dateStr, int amount) { String nowDate = dateStr; if ((nowDate == null) || (nowDate.trim().equals(""))) { nowDate = getCurrentDay("yyyy-MM-dd"); } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar nowCalendar = Calendar.getInstance(); try { nowCalendar.setTime(sdf.parse(nowDate)); } catch (ParseException e) { e.printStackTrace(); } nowCalendar.add(5, amount); Timestamp timestamp = new Timestamp(nowCalendar.getTimeInMillis()); return timestamp; } public static Timestamp rollMonthTimestamp(String dateStr, int amount) { String nowDate = dateStr; if ((nowDate == null) || (nowDate.trim().equals(""))) { nowDate = getCurrentDay("yyyy-MM-dd"); } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar nowCalendar = Calendar.getInstance(); try { nowCalendar.setTime(sdf.parse(nowDate)); } catch (ParseException e) { e.printStackTrace(); } nowCalendar.add(2, amount); Timestamp timestamp = new Timestamp(nowCalendar.getTimeInMillis()); return timestamp; } public static String format(Timestamp time, String formatStr) { if (time == null) { return null; } SimpleDateFormat format = new SimpleDateFormat(formatStr); String date = format.format(time); return date; } public static String format(String time, String formatStr) { if (StrUtil.isNull(time)) { return null; } String date = ""; try { SimpleDateFormat format = new SimpleDateFormat(formatStr); date = format.format(new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time).getTime())); } catch (ParseException e) { e.printStackTrace(); return ""; } return date; } public static String getMonthStr(int month) { String monthStr = ""; switch (month) { case 1: monthStr = "一月"; break; case 2: monthStr = "二月"; break; case 3: monthStr = "三月"; break; case 4: monthStr = "四月"; break; case 5: monthStr = "五月"; break; case 6: monthStr = "六月"; break; case 7: monthStr = "七月"; break; case 8: monthStr = "八月"; break; case 9: monthStr = "九月"; break; case 10: monthStr = "十月"; break; case 11: monthStr = "十一月"; break; case 12: monthStr = "十二月"; } return monthStr; } public static int getWeekOfYear() { int week = 0; Calendar c = Calendar.getInstance(); week = c.get(3); return week; } public static String getStartDate(int year, int weekNo) { Calendar cal = Calendar.getInstance(); cal.set(1, year); cal.set(3, weekNo); cal.set(7, 2); SimpleDateFormat returnDate = new SimpleDateFormat("yyyy年MM月dd日"); return returnDate.format(cal.getTime()); } public static String getEndDate(int year, int weekNo) { Calendar cal = Calendar.getInstance(); cal.set(1, year); cal.set(3, weekNo + 1); cal.set(7, 1); SimpleDateFormat returnDate = new SimpleDateFormat("yyyy年MM月dd日"); return returnDate.format(cal.getTime()); } public static Collection getTwoDateDayList(String endDate, String beginDate) { Collection resultList = new ArrayList(); long dayCount = getDayBetween(endDate, beginDate); resultList.add(beginDate); for (int i = 0; i < dayCount; i++) { Timestamp now = rollDayTimestamp(beginDate, 1); beginDate = format(now, "yyyy-MM-dd"); resultList.add(beginDate); } return resultList; } public static Collection getTwoDateDayListDesc(String endDate, String beginDate) { Collection resultList = new ArrayList(); long dayCount = getDayBetween(endDate, beginDate); resultList.add(endDate); for (long i = dayCount; i > 0L; i -= 1L) { Timestamp now = rollDayTimestamp(endDate, -1); endDate = format(now, "yyyy-MM-dd"); resultList.add(endDate); } return resultList; } public static String getWeekForDate(String date, String format) { String week = ""; SimpleDateFormat f1 = new SimpleDateFormat(format); Date d = null; try { d = f1.parse(date); } catch (ParseException e) { e.printStackTrace(); } SimpleDateFormat formatter = new SimpleDateFormat("E"); week = formatter.format(d); return week; } public static String getStandardDate(int year, int month, int day) { String yearStr = String.valueOf(year); String monthStr = String.valueOf(month); String dayStr = String.valueOf(day); if (month < 10) { monthStr = "0" + month; } if (day < 10) { dayStr = "0" + day; } return yearStr + "-" + monthStr + "-" + dayStr; } public static String rollHoursTimestamp(String dateStr, String amount) { SimpleDateFormat sdf = new SimpleDateFormat(dateStr); String timeStr = getCurrentDay(dateStr); Date time = null; try { time = sdf.parse(timeStr); } catch (ParseException e) { e.printStackTrace(); } int hours = StrUtil.getNotNullIntValue(amount, 0); Date torollHours = new Date(time.getTime() - hours * 3600 * 1000); return sdf.format(torollHours).toString(); } public static String rollMinutesTimestamp(String dateStr, String amount) { SimpleDateFormat sdf = new SimpleDateFormat(dateStr); String timeStr = getCurrentDay(dateStr); Date time = null; try { time = sdf.parse(timeStr); } catch (ParseException e) { e.printStackTrace(); } int minutes = StrUtil.getNotNullIntValue(amount, 0); Date torollMinutes = new Date(time.getTime() - minutes * 60 * 1000); return sdf.format(torollMinutes).toString(); } public static boolean compareTotime(String dateStr) { boolean isOK = false; long count = 0L; SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowtimeStr = getCurrentDay("yyyy-MM-dd HH:mm:ss"); try { Date nowDate = myFormatter.parse(nowtimeStr); Date foreDate = myFormatter.parse(dateStr); count = nowDate.getTime() - foreDate.getTime(); } catch (ParseException e) { count = 0L; e.printStackTrace(); } if (count > 0L) { isOK = true; } return isOK; } public static boolean compareTotime(String oneStr, String twoStr) { boolean isOK = false; long count = 0L; SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date nowDate = myFormatter.parse(oneStr); Date foreDate = myFormatter.parse(twoStr); count = nowDate.getTime() - foreDate.getTime(); } catch (ParseException e) { count = 0L; e.printStackTrace(); } if (count > 0L) { isOK = true; } return isOK; } public static Timestamp getNotNullTimestampValue(String data, String dateFormat) { Timestamp value; try { Timestamp value; if ((data == null) || (data.equals(""))) { value = new Timestamp(System.currentTimeMillis()); } else { SimpleDateFormat smd = new SimpleDateFormat(dateFormat); value = new Timestamp(smd.parse(data).getTime()); } } catch (Exception e) { Timestamp value; e.printStackTrace(); value = new Timestamp(System.currentTimeMillis()); } return value; } public static int compareTimstampStr(String sourceStr, String destStr, String formartStr) { int result = 10; SimpleDateFormat dateFormatter = new SimpleDateFormat(formartStr); long temp = 10L; try { Date beginDate = dateFormatter.parse(sourceStr); Date endDate = dateFormatter.parse(destStr); temp = beginDate.getTime() - endDate.getTime(); } catch (ParseException e) { result = 2147483647; e.printStackTrace(); } if (temp > 0L) result = 1; else if (temp == 0L) result = 0; else if (temp < 0L) { result = -1; } return result; } public static String getDateFormat(String date) { Timestamp nowDay = getTodayTimestamp(); String hour = date.substring(11, date.length()); date = format(date, "yyyy-MM-dd"); if (date.equals(getCurrentDay("yyyy-MM-dd"))) { return hour; } if (nowDay.equals(rollDayTimestamp(date, 1))) { return "昨天 " + hour; } if (nowDay.equals(rollDayTimestamp(date, 2))) { return "前天 " + hour; } return date + " " + hour; } public static int[] getDayBetweenForYMD(String endDate, String beginDate) { int endDateY = Integer.parseInt(format(endDate, "yyyy", "yyyy-MM-dd")); int endDateM = Integer.parseInt(format(endDate, "MM", "yyyy-MM-dd")); int endDateD = Integer.parseInt(format(endDate, "dd", "yyyy-MM-dd")); int beginDateY = Integer.parseInt(format(beginDate, "yyyy", "yyyy-MM-dd")); int beginDateM = Integer.parseInt(format(beginDate, "MM", "yyyy-MM-dd")); int beginDateD = Integer.parseInt(format(beginDate, "dd", "yyyy-MM-dd")); int mS = (endDateY - beginDateY) * 12 + endDateM - beginDateM; int y = mS / 12; int m = mS % 12; int d = (int)getDayBetween(getStandardDate(endDateY, endDateM, endDateD), getStandardDate(endDateY, endDateM, beginDateD)); if (d < 0) { m--; d += getMonthLastDay(endDateY, endDateM - 1); } if (m < 0) { y--; m += 12; } return new int[] { y, m, d }; } public static int getMonthLastDay(int year, int month) { Calendar a = Calendar.getInstance(); a.set(1, year); a.set(2, month - 1); a.set(5, 1); a.roll(5, -1); int maxDate = a.get(5); return maxDate; } public static String format(String time, String formatStr, String timeformatStr) { if (StrUtil.isNull(time)) { return null; } String date = ""; try { SimpleDateFormat format = new SimpleDateFormat(formatStr); date = format.format(new Timestamp(new SimpleDateFormat(timeformatStr).parse(time).getTime())); } catch (ParseException e) { e.printStackTrace(); return ""; } return date; } public static boolean checkDate(String date, String format) { SimpleDateFormat df = new SimpleDateFormat(format); Date d = null; try { d = df.parse(date); } catch (Exception e) { return false; } String s1 = df.format(d); return date.equals(s1); } }
例如:
public Timestamp getCONFIRM_TIME() { return CONFIRM_TIME; } public String getConfirmTime(String format){ return DateUtil.format(this.getCONFIRM_TIME(), format); } public void setCONFIRM_TIME(Timestamp cONFIRM_TIME) { CONFIRM_TIME = cONFIRM_TIME; }
在实体类里面添加个String方法Timestamp就可以转String了 format是时间格式,如:
jsonNew.put("delivery_time", order.getDeliveryTime("yyyy-MM-dd HH:mm:ss")); jsonNew.put("confirm_time", order.getConfirmTime("yyyy-MM-dd HH:mm:ss"));
相关推荐
时间戳Timstamp时间双向转换工具 Timestamp转时间,时间转Timestamp。
1. 字符串日期时间转换成时间戳 # '2015-08-28 16:43:37.283' --> 1440751417.283 # 或者 '2015-08-28 16:43:37' --> 1440751417.0 def string2timestamp(strValue): try: d = datetime.datetime.strptime...
不懂这块的看看啦,mysql中中四中时间类型之一的TimeStamp,在项目中的简单运用,如何将TimeStamp转换成自己需要的时间格式
微小的工作流程tiny-time-workflow是一个alfred工作流,用于在datetime和timestamp之间转换时间表示。关键词功能tt 日期时间到时间戳特诺获取当前时间戳tdt 时间戳记到日期时间德诺获取当前日期时间屏幕截图tt 特诺 ...
功能:将1970-01-01 00:00:00以来的毫秒数转换为对应的timestamp时间类型,精确保留毫秒级精度! 参数:I_MILLISECONDS NUMBER 待转换的毫秒数 示例:select MILLISECONDS2TIMESTAMP(1397457489296) from dual; ...
Unix系统下时间戳的设置
常见的时间字符串与timestamp之间的转换 日期与timestamp之间的转换 常见的时间字符串与timestamp之间的转换 这里说的字符串不是一般意义上的字符串,是指在读取日期类型的数据时,如果还没有及时解析字符串,它就还...
Timstamp时间戳转日期时间双向转换工具,Timestamp转时间,时间转Timestamp。
本文将详细介绍oracle SCN跟TIMESTAMP之间转换,需要的朋友可以参考下
主要介绍了C#更新SQLServer中TimeStamp字段(时间戳)的方法,涉及C#操作数据库字段的相关技巧,需要的朋友可以参考下
内容概要:日期时间转换工具类,包括基本的Date类型,String类型,TimeStamp类型,LocalDateTime类型,LocalDate类型之间的互相转换,还提供了许多与时间获取,时间计算有关的方法,如:获取指定日期几天后的日期;...
Oracle中的时间是Date型,以下函数提供了两种时间转换的Oracle函数 --unix时间戳与date时间互转 SELECT oracle_to_unix(SYSDATE),unix_to_oracle(1551774286),oracle_to_unix(SYSDATE) FROM dual;
通过matlab读取从传感器中获取到的数据文件(excel格式),将时间戳转换为标准时间
强大的好用的原创日期...7.根据指定时间格式将Timestamp对象转换为字符串 8.根据默认时间格式将Timestamp对象转换为字符串 9.将Timestamp对象转换为Date对象 10.返回两个时间字符串的小时差 11.返回多少小时之前的时间
UTC时间转换,最终得到的都是UTC时间。 简单来说就是: 时间戳(timestamp) 转换-> UTC显示时间(datetime),使用time.gmtime(timestamp)。 显示时间(datetime) 转换-> UTC时间戳(timestamp),使用calendar....
我们看到, 直接在可视化工具里用SQL写...甚至去掉 TIMESTAMP 都是可以的, pgSQL可以自动把字符串给你转换成日期格式, 然后作比较 但是在mapper文件中这么写就不可以了, 它会提示你varchar类型不能和日期类型进行比较
本文实例讲述了python正常时间和unix时间戳相互转换的方法。分享给大家供大家参考。具体分析如下: 这段代码可以用来转换常规时间格式为unix时间戳,也可以将unix时间戳转换回来, 例如:1332888820 格式转换成 ...
常见的时间字符串与timestamp之间的转换 日期与timestamp之间的转换 常见的时间字符串与timestamp之间的转换 这里说的字符串不是一般意义上的字符串,是指在读取日期类型的数据时,如果还没有及时解析字符串,它就还...
Unix时间戳和时间之间的转换工具 将2017-08-09转换成时间戳,以及将Unix时间戳转换成20170-08-09的格式化时间