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"));
相关推荐
在实际应用中,我们常常需要将从数据库或其他来源获取的字符串形式的日期时间转换为时间戳。以下是一个示例函数,用于将字符串日期时间转换为时间戳: ```python import time from datetime import datetime def ...
`mktime()`函数可以将结构体`tm`表示的本地时间转换为时间戳。 在UTC和北京时间的转换中,关键在于理解时区差异。由于北京位于东八区,所以要将UTC时间转换为北京时间,只需在UTC时间戳基础上加8小时;反之,若要将...
然而,在实际开发中,我们经常需要将String类型转换成Timestamp类型,以便于在数据库中存储或操作时间相关的数据。本文将介绍如何将String类型转换成Timestamp类型,并对相关的知识点进行详细的解释。 首先,让我们...
相反,将日期和时间转换为时间戳,我们可以使用`timestamp()`函数: ```python dt_object = datetime.datetime(2022, 1, 1, 0, 0) # 假设这是个日期时间对象 timestamp = dt_object.timestamp() print(timestamp) #...
在Oracle数据库中,`TIMESTAMP`与`DATE`两种数据类型是用于存储日期和时间信息的关键组成部分,但它们之间存在显著的区别,特别是在处理时间和精度方面。本文将深入探讨这两种数据类型的特点,以及如何在实际应用中...
Date、String、Timestamp 是 Java 中三个常用的日期和时间类,它们之间的转换是 Java 开发中经常遇到的问题。在本文中,我们将详细介绍 Date、String、Timestamp 之间的转换方法。 Date 和 String 之间的转换 在 ...
时间戳(Timestamp)是计算机系统中用来记录时间的一种方式,它表示从某个固定时间点(通常是1970年1月1日00:00:00 UTC)到当前时刻的秒数,或者毫秒数。在IT行业中,时间戳被广泛用于各种数据存储、日志记录以及...
在Java编程中,Timestamp和Date是两个常用的时间类型,前者是数据库类型,而后者是Java.util包中的类。由于它们的类型不同,因此在实际开发中,经常需要将Timestamp和Date类型相互转换。下面将详细介绍Timestamp和...
Unix系统下时间戳的设置
- **时间转Time Ticks**:将普通时间转换成Time Ticks,你需要知道系统启动的时间(即基点时间),然后计算出目标时间与基点时间的差值,以“滴答”为单位。这通常涉及到日期和时间的数学运算,例如减去基点时间的...
为了准确地进行时间和地理位置的计算,理解它们之间的转换至关重要。这里我们将详细探讨GPS UTC时间与北京时间的转换方法,并分享相关的编程实现。 首先,我们需要了解两者的定义和区别。UTC是国际电信联盟(ITU)...
功能:将1970-01-01 00:00:00以来的毫秒数转换为对应的timestamp时间类型,精确保留毫秒级精度! 参数:I_MILLISECONDS NUMBER 待转换的毫秒数 示例:select MILLISECONDS2TIMESTAMP(1397457489296) from dual; ...
在这种情况下,可以使用`ZonedDateTime`的`toInstant()`方法将时间转换为UTC的`Instant`对象,或者使用`ZonedDateTime.atZoneSameInstant(ZoneId.systemDefault())`将其转换为本地时区。 此外,`java.time.format....
在Oracle 10g及以后的版本中,为了支持闪回操作和其他时间相关的功能,引入了SCN与TIMESTAMP之间的转换函数。这些函数允许我们根据需要将SCN值转换为可读的日期时间,反之亦然。 1. **获取当前SCN**: 你可以使用 ...
在IT领域,时间转换是一个常见的任务,特别是在数据分析和系统集成中。本文将深入探讨"时间转换工具及源码"的相关知识点,包括EXCEL时标、UNIX时标和DATETIME之间的转换,以及如何利用源码进行自定义处理。 首先,...
微小的工作流程tiny-time-workflow是一个alfred工作流,用于在datetime和timestamp之间转换时间表示。关键词功能tt 日期时间到时间戳特诺获取当前时间戳tdt 时间戳记到日期时间德诺获取当前日期时间屏幕截图tt 特诺 ...
然而,根据实际需求,我们可能需要将这些时间数据转换为不同的格式,或者从字符串形式解析到日期类型,这就需要用到时间转换器。 1. TO_DATE函数:这是将字符串转换为DATE类型的主要函数。例如,`TO_DATE('2022-01-...
在C#编程中,SQL Server的时间戳(TimeStamp)字段是一个特殊的数据类型,它与我们通常理解的日期时间无关,而是用来记录数据行的版本或更改信息。本文将深入探讨如何在C#中读取和更新SQL Server中的Timestamp字段。...
在IT行业中,时间转换是常见的任务,特别是在处理日期和时间数据时。这个“一个long型与Date型时间转换工具”正是为了简化这类操作而设计的。Long类型在Java中通常用来存储时间戳,即从1970年1月1日(UTC)开始到...