Java 中的 Timestamp 对应 mysql 中的 dateTime 类型
比如:java 的 bean 类型是 Timestamp
插入的时候这么写,这样插入完成后。mysql 数据库里就是 datatime 类型的数据了。
setUpdateTime(new Timestamp(new Date().getTime()));
|
也可以分开写如下:
java.util.Date date = new java.util.Date(); Timestamp timeStamp = new Timestamp(date.getTime());
|
Java:String 和 Date、Timestamp 之间的转换
String 与 Date(java.util.Date)互转
String -> Date
String dateStr = "2010/05/04 12:34:23"; Date date = new Date();
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); try { date = sdf.parse(dateStr); System.out.println(date.toString()); } catch (Exception e) { e.printStackTrace(); }
|
Date -> String
String dateStr = ""; Date date = new Date(); DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss"); try { dateStr = sdf.format(date); System.out.println(dateStr); dateStr = sdf2.format(date); System.out.println(dateStr); } catch (Exception e) { e.printStackTrace(); }
|
String 与 Timestamp 互转
String ->Timestamp:使用 Timestamp 的 valueOf()方法
注:String 的类型必须形如: yyyy-mm-dd hh:mm:ss[.f…] 这样的格式,中括号表示可选,否则报错!!!
如果 String 为其他格式,可考虑重新解析下字符串,再重组~~
Timestamp ts = new Timestamp(System.currentTimeMillis()); String tsStr = "2011-05-09 11:49:45"; try { ts = Timestamp.valueOf(tsStr); System.out.println(ts); } catch (Exception e) { e.printStackTrace(); }
|
Timestamp -> String:使用 Timestamp 的 toString()方法或者借用 DateFormat
Timestamp ts = new Timestamp(System.currentTimeMillis()); String tsStr = ""; DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); try { tsStr = sdf.format(ts); System.out.println(tsStr); tsStr = ts.toString(); System.out.println(tsStr); } catch (Exception e) { e.printStackTrace(); }
|
Date( java.util.Date )和 Timestamp 互转
声明:查 API 可知,Date 和 Timestamp 是父子类关系,Date 是 Timestamp 的父类
Timestamp -> Date
ate 对象指向的实体是一个 Timestamp,即 date 拥有 Date 类的方法,但被覆盖的方法的执行实体在 Timestamp 中。
Timestamp ts = new Timestamp(System.currentTimeMillis()); Date date = new Date(); try { date = ts; System.out.println(date); } catch (Exception e) { e.printStackTrace(); }
|
Date -> Timestamp(父类不能直接向子类转化)
Timestamp ts = new Timestamp(date.getTime());
|
以上都是旧的。过期的,因为 Date 类是非线程安全的
在 java8 里
mysql 的 date 对应 java 的 LoacalDate
mysql 的 dateTime 对应 java 的 LocalDateTime
获取 LoacalDateTime 或 LocalDate
LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime); LocalDate localDate = LocalDate.now(); System.out.println(localDate);
|
LocalDateTime 与 LocalDate 获取时间戳(如果要比较大小的话可以用计算出的时间戳来比较)
ZoneId.systemDefault()
ZoneId bjZone = ZoneId.of("GMT+08:00")
|
LocalDate localDate = LocalDate.now();
LocalDateTime localDateTime = localDate.atStartOfDay();
long l1 = localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond();
long l2 = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); System.out.println("秒 : "+l1); System.out.println("毫秒 : "+l2);
long l = LocalDateTime.now().atZone(ZoneId.systemDefault()).toEpochSecond();
long l1 = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); System.out.println("秒: "+l); System.out.println("毫秒: "+l1);
|
LocalDateTime 与 LocalDate 的相互转化
LocalDateTime now = LocalDateTime.now(); LocalDate localDate = now.toLocalDate();
LocalDate localDate = LocalDate.now(); LocalDateTime localDateTime1 = localDate.atStartOfDay();
LocalDateTime localDateTime2 = localDate.atTime(23,1,2);
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime3 = localDate.atTime(localTime); System.out.println("atStartOfDay:日期后面拼接 00点00分 : "+localDateTime System.out.println("atTime:参数分别代表 时、分、秒: "+localDateTime2); System.out.println("atTime:拼接当前localTime : "+localDateTime3);
|
LocalDateTime 与 String 的相互转化
DateTimeFormatter df = DateTimeFormatter.ofPattern(``"yyyy-MM-dd HH:mm:ss"``);
LocalDateTime time = LocalDateTime.now();
String localTime = df.format(time);`
System.out.println(``"LocalDateTime转成String类型的时间:"``+localTime);
LocalDateTime LocalTime = LocalDateTime.parse(localTime,df)
System.out.println(``"String类型的时间转成LocalDateTime:"``+LocalTime);
|
LocalDate 与 String 的相互转化
DateTimeFormatter struct = DateTimeFormatter.ofPattern(``"yyyy-MM-dd"``)
LocalDate localDate = LocalDate.now();
String format = struct.format(localDate)
System.out.println(``"LocalDate转成String类型的时间:"``+format)
LocalDate parse = LocalDate.parse(format,struct)
System.out.println(``"String类型的时间转成LocalDateTime:"``+parse);
|
LocalDateTime 与 Date 的相互转化
java.util.Date date =` `new` `java.util.Date();
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
LocalDateTime localDateTime = LocalDateTime.now();
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
java.util.Date date = Date.from(instant);
|
LocalDate 与 Date 的相互转化
LocalDate localDate = LocalDate.now();
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
java.util.Date date = Date.from(instant);
Date date = new Date(); Instant instant = date.toInstant(); ZoneId zoneId = ZoneId.systemDefault();
LocalDate localDate = instant.atZone(zoneId).toLocalDate();
|
调整时间
LocalDateTime now = LocalDateTime.now();
LocalDateTime plusDays = now.plusDays(1);
LocalDateTime plusDays2 = now.plusDays(-1);
|
间隔计算
使用 Duration 进行 day,hour,minute,second 等的计算
使用 Period 进行 Year,Month 的计算
Duration duration = Duration.between(localDateTime,localDateTime4); duration.toDays(); duration.toHours(); duration.toMinutes();
Period period2 = Period.between(localDateTime.toLocalDate(),localDateTime4.toLocalDate()); period2.getYears(); period2.getMonths(); period2.toTotalMonths();
|
判断是否是今天或昨天
String time = "2018-08-09 11:20:45"; DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"); LocalDateTime localTime = LocalDateTime.parse(time, dtf); LocalDateTime startTime = LocalDate.now().atTime(0, 0, 0); LocalDateTime endTime = LocalDate.now().atTime(23, 59, 59); LocalDateTime startYesterday = LocalDate.now().plusDays(-1).atTime(0, 0, 0); LocalDateTime endYesterday = LocalDate.now().plusDays(-1).atTime(23, 59, 59);
if (localTime.isBefore(startYesterday)) { System.out.println("时间是过去"); } if (localTime.isAfter(startYesterday) && localTime.isBefore(endYesterday)) { System.out.println("时间是昨天"); } if (localTime.isAfter(startTime) && localTime.isBefore(endTime)) { System.out.println("时间是今天"); } if (localTime.isAfter(endTime)) { System.out.println("时间是未来"); } }
|
循环某段时间
LocalDate start = LocalDate.of(2019, 4, 29); LocalDate end = LocalDate.of(2019, 7, 29); do { System.out.println(start); start = start.plusDays(1); } while (start.toEpochDay() <= end.toEpochDay());
|
获取最近 7 天每天开始和结束时间戳
LocalDate start = LocalDate.now().plusDays(-7); LocalDate end = LocalDate.now(); do { start.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); start.atTime(LocalTime.MAX).atZone(ZoneId.systemDefault()) .toInstant().toEpochMilli(); start = start.plusDays(1); } while (start.toEpochDay() <= end.toEpochDay());
|