Some Methods of Common Time in Java
- 2021-12-04 18:30:48
- OfStack
Preface 1. The way to get the current time 2. Get the n day of the current month 3. Format it as a string 4. Add and subtract time (the unit can be seconds, hours, etc.) 5. Get the age through the date of birth 6. Judge whether the two time periods cover 7. Seek two time intervals 8. UTC time and Beijing time conversion summary
Preface
In our java development, Date date this field will be frequently used, such as getting the time of the current system, getting the time of last month, last year, and getting the time of the difference between two dates, or formatting the date type, etc., etc., the following will give you a detailed introduction to the next Java commonly used time 1 related methods
1. How to get the current time
public static void main(String[] args) {
//Date
Date now = new Date();
System.out.println(now);
//java8 Time of
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
Calendar calendar = Calendar.getInstance();
Date time = calendar.getTime();
System.out.println(time);
System.out.println(" Year " + calendar.get(Calendar.YEAR));
System.out.println(" Month " + (calendar.get(Calendar.MONTH) + 1));
//joda time
DateTime dateTime = DateTime.now();
System.out.println(dateTime);
}
You can use Date LocalDatetime Calendar Datetime to get the current time
2. Get the n day of the current month
public static void main(String[] args) {
// Recommended use Calendar You can set year, month, day, hours and seconds
Calendar calendar = Calendar.getInstance();
//// Current month 16
calendar.set(Calendar.DAY_OF_MONTH, 16);
System.out.println(calendar.getTime());
// Current month 16
DateTime now = DateTime.now();
DateTime dateTime = now.withDayOfMonth(16);
System.out.println(dateTime);
// Current month 14
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime.withDayOfMonth(14));
//1 Month 11
System.out.println(localDateTime.withMonth(1).withDayOfMonth(11));
}
3. Format as a string
```
// Use SimpleDateFormat
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(format.format(new Date()));
// Use Calendar
Calendar calendar = Calendar.getInstance();
System.out.println(String.format("%s Year %s Month %s Day %s Hour %s Points %s Seconds ", calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH),
calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)));
LocalDateTime now = LocalDateTime.now();
String str = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(str);
```
4. Addition and subtraction time (the unit can be seconds, hours, etc.)
public static void main(String[] args) {
Date now = new Date();
// Plus 1 Hours
long time = now.getTime() + (60 * 60 * 1000);
System.out.println(new Date(time));
/*
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.14</version>
</dependency>
*/
// Introduce Hutool Plus 1 Hours
System.out.println(DateUtil.offset(now, DateField.HOUR, 1));
// Minus 1 Hours
System.out.println(DateUtil.offset(now, DateField.HOUR, -1));
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(" Plus 1 Hours " + localDateTime.plusHours(1));
System.out.println(" Minus 1 Hours " + localDateTime.minusHours(1));
DateTime dateTime = DateTime.now();
System.out.println(dateTime.plusHours(1));
System.out.println(dateTime.minusHours(1));
}
Both LocalDateTime and DateTime have their own methods to increase and decrease time
5. Age by date of birth
public static void main(String[] args) {
// Time 1990-12-05
DateTime birthDay = DateTime.now().withYear(1990).withMonthOfYear(10).withDayOfMonth(23);
System.out.println(birthDay);
// Acquisition difference year Month and date comparisons are made Such as
Years years = Years.yearsBetween(birthDay, new DateTime());
System.out.println(years);
System.out.println(years.getYears());
}
You can also use the method of subtracting the year and comparing the month and day to get the birthday
6. Determine whether the two time periods are covered
public static void main(String[] args) {
DateTime now = DateTime.now();
DateTime start1 = now;
DateTime end1 = now.plusMinutes(1);
DateTime start2 = now.plusSeconds(50);
DateTime end2 = now.plusMinutes(2);
Interval interval1 = new Interval(start1, end1);
Interval interval2 = new Interval(start2, end2);
System.out.println(interval1.overlaps(interval2));
System.out.println(start1.getMillis() < end2.getMillis() && start2.getMillis() < end1.getMillis());
}
7. Find two time intervals
public static void main(String[] args) {
DateTime now = DateTime.now();
// Start time
Date startTime = now.toDate();
// End time
Date endTime = now.plusHours(1).toDate();
//1 Hours
System.out.println(" Time interval between start time and end time :" + DateUtil.between(startTime, endTime, DateUnit.SECOND));
long time = (endTime.getTime() - startTime.getTime()) / 1000;
System.out.println(time);
}
8. Conversion between UTC time and Beijing time
public static void main(String[] args) throws ParseException {
Date now = new Date();
Date utcDate = bj2UTC(now);
//utc Time
System.out.println(utcDate);
// Beijing time
System.out.println(utc2BJ(utcDate));
DateTime dateTime = DateTime.now().withDayOfMonth(1).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0);
System.out.println(dateTime);
System.out.println(bj2UTC(dateTime.toDate()));
}
public static Date bj2UTC(Date date) {
if (date == null) {
return null;
}
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.of("-8"));
return Date.from(localDateTime.atOffset(ZoneOffset.UTC).toInstant());
}
public static Date utc2BJ(Date date) {
if (date == null) {
return null;
}
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.of("+8"));
return Date.from(localDateTime.atOffset(ZoneOffset.UTC).toInstant());
}
Beijing time = UTC+8