public class SimpleDateFormatDemo {
public static void main ( String [ ] args) throws ParseException {
String time = "2022-05-21 12:23:23" ;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
System . out. println ( simpleDateFormat. parse ( time) ) ;
method ( ) ;
}
private static void method ( ) {
Date d = new Date ( ) ;
SimpleDateFormat sim = new SimpleDateFormat ( "yyyy/MM/dd HH:mm:ss" ) ;
System . out. println ( sim. format ( d) ) ;
}
}
public class SimpleDateFormatTest {
public static void main ( String [ ] args) throws ParseException {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请录入你的生日(xxxx-xx-xx)" ) ;
String birthday = sc. next ( ) ;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Date date = simpleDateFormat. parse ( birthday) ;
long start = date. getTime ( ) ;
Date date1 = new Date ( ) ;
long end = date1. getTime ( ) ;
System . out. println ( "你活了多少" + ( end - start) / ( 1000L * 60 * 60 * 24 * 365 ) ) ;
}
}
public class LocalDateTimeDemo2 {
public static void main ( String [ ] args) {
LocalDateTime now = LocalDateTime . now ( ) ;
now. getYear ( ) ;
now. getMonthValue ( ) ;
now. getDayOfMonth ( ) ;
now. getDayOfYear ( ) ;
now. getDayOfWeek ( ) ;
now. getHour ( ) ;
now. getMinute ( ) ;
now. plusYears ( 1 ) ;
now. withMonth ( 6 ) ;
}
}
public class LocalDateTimeDemo4 {
public static void main ( String [ ] args) {
LocalDateTime localDateTime = LocalDateTime . now ( ) ;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter . ofPattern ( "yyyy/MM/dd HH:mm:ss" ) ;
String format = localDateTime. format ( dateTimeFormatter) ;
}
}
public static void main ( String [ ] args) {
String str = "2021/11/11 12:12:12" ;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter . ofPattern ( "yyyy/MM/dd HH:mm:ss" ) ;
LocalDateTime . parse ( str, dateTimeFormatter) ;
}
public class PeriodAndDuration {
public static void main ( String [ ] args) {
LocalDate start = LocalDate . of ( 2021 , 11 , 11 ) ;
LocalDate end = LocalDate . of ( 2022 , 12 , 12 ) ;
Period between = Period . between ( start, end) ;
between. getYears ( ) ;
between. getMonths ( ) ;
between. getDays ( ) ;
LocalDateTime of1 = LocalDateTime . of ( 2022 , 12 , 23 , 12 , 23 , 12 ) ;
LocalDateTime of2 = LocalDateTime . of ( 2020 , 10 , 23 , 12 , 21 , 12 ) ;
Duration between1 = Duration . between ( of1, of2) ;
System . out. println ( between1. toDays ( ) ) ;
System . out. println ( between1. toHours ( ) ) ;
System . out. println ( between1. toMinutes ( ) ) ;
}
}