获取当前时间(Date类型)
1 2 3 4 5 6
| import java.util.Date; public static void main(String[] args) { Date nowDate = new Date(); System.out.println(nowDate); }
|
输出:Mon Apr 12 16:09:29 CST 2021
获取当前时间(Instant类型)
1 2 3 4 5 6 7 8
| import java.time.Instant; import java.util.concurrent.TimeUnit; public static void main(String[] args) { Instant nowDate = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)); System.out.println(nowDate); }
|
输出:2021-04-12T16:33:18.334Z
获取当前时间毫秒数(System获取、Date获取、Calendar获取、Instant获取)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import java.time.Instant; import java.util.Calendar; import java.util.Date; import java.util.concurrent.TimeUnit; public static void main(String[] args) { System.out.println(System.currentTimeMillis()); Date date = new Date(); System.out.println(date.getTime()); Calendar calendar = Calendar.getInstance(); long time = calendar.getTimeInMillis(); System.out.println(time); Instant now = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)); System.out.println(now.toEpochMilli()); }
|
输出:1618217202418 1618217202419 1618217202425 1618246002438
获取当前年月(Calendar类型)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import java.util.Calendar; public static void main(String[] args) { Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH ) + 1; int day = cal.get(Calendar.DATE); String nowDate = year + "-" + month + "-" + day; System.out.println(nowDate); }
|
获取当前年月(Calendar类型),月份自动填0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import java.util.Calendar; public static void main(String[] args) { Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH ) + 1; String month2 = month<10 ? "0" + month : String.valueOf(month); int day = cal.get(Calendar.DATE); String nowDate = year + "-" + month2 + "-" + day; System.out.println(nowDate); }
|
获取当前日期(LocalDate类型)
1 2 3 4 5 6
| import java.time.LocalDate; public static void main(String[] args) { LocalDate today = LocalDate.now(); System.out.println(today); }
|
获取当前时间(LocalDateTime类型)
1 2 3 4 5 6
| import java.time.LocalDateTime; public static void main(String[] args) { LocalDateTime today = LocalDateTime.now(); System.out.println(today); }
|
输出:2021-04-12T16:37:25.244
获取当前时间(Timestamp类型)
1 2 3 4 5 6 7 8 9
| import java.sql.Timestamp; import java.text.SimpleDateFormat; public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Timestamp timestamp = new Timestamp(System.currentTimeMillis()); String now = sdf.format(timestamp); System.out.println(now); }
|
Date类型 与 String类型转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); String now = sdf.format(date); System.out.println(now); Date now2 = sdf.parse("2021-04-12 17:30:54"); System.out.println(now2); }
|
DateTime类型 转 String类型
1 2 3 4 5
| public static void main(String[] args) { DateTime dateTime = new DateTime(); String nowTime = dateTime.toString("yyyy-MM-dd HH:mm:ss"); System.out.println(nowTime); }
|
Date类型 转 Instant类型
1 2 3 4 5 6 7 8
| import java.time.Instant; import java.util.Date; public static void main(String[] args) { Date date = new Date(); Instant instant = date.toInstant(); System.out.println(instant); }
|
DateTime类型 与 Calendar类型转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import java.util.Calendar; import java.util.Date; public static void main(String[] args) { Calendar cal = Calendar.getInstance(); Date date = cal.getTime(); System.out.println(date); Date date2=new Date(); Calendar cal2 = Calendar.getInstance(); cal.setTime(date2); System.out.println(cal2); }
|
输出:Mon Apr 12 17:27:11 CST 2021
输出:java.util.GregorianCalendar[
time=1618219631202,
areFieldsSet=true,
areAllFieldsSet=true,
lenient=true,
zone=sun.util.calendar.ZoneInfo[
id=”Asia/Shanghai”,
offset=28800000,
dstSavings=0,
useDaylight=false,
transitions=29,
lastRule=null
],
firstDayOfWeek=1,
minimalDaysInFirstWeek=1,
ERA=1,
YEAR=2021,
MONTH=3,
WEEK_OF_YEAR=16,
WEEK_OF_MONTH=3,
DAY_OF_MONTH=12,
DAY_OF_YEAR=102,
DAY_OF_WEEK=2,
DAY_OF_WEEK_IN_MONTH=2,
AM_PM=1,
HOUR=5,
HOUR_OF_DAY=17,
MINUTE=27,
SECOND=11,
MILLISECOND=202,
ZONE_OFFSET=28800000,
DST_OFFSET=0
]
String类型 与 Instant类型转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Locale; public static void main(String[] args) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); Instant instant = Instant.now(); ZoneId zoneId = ZoneId.systemDefault(); LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId); String now = dtf.format(localDateTime); System.out.println(now); String string = "2021-04-12T23:59:59.123Z"; DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.FRANCE); LocalDateTime date = LocalDateTime.parse(string, dtf2); System.out.println(date); }
|
LocalDate类型 转换 Date类型
1 2 3 4 5 6 7 8 9 10 11 12
| import java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; import java.util.Date; public static void main(String[] args) { LocalDate localDate = LocalDate.now(); ZoneId zone = ZoneId.systemDefault(); Instant instant = localDate.atStartOfDay().atZone(zone).toInstant(); Date date = Date.from(instant); System.out.println(date); }
|
输出:Mon Apr 12 00:00:00 CST 2021