时间类应用
package Demo001;
public class Demo01date {
public static void main ( String[ ] args) {
System. out. println ( System. currentTimeMillis ( ) ) ;
}
}
package Demo001;
import java. util. Date;
public class Demo02date {
public static void main ( String[ ] args) {
demo01 ( ) ;
demo02 ( ) ;
demo03 ( ) ;
}
private static void demo01 ( ) {
Date date = new Date ( ) ;
System. out. println ( date) ;
}
private static void demo02 ( ) {
Date date = new Date ( 0 L) ;
System. out. println ( date) ;
date = new Date ( 1618619905988 L) ;
System. out. println ( date) ;
}
private static void demo03 ( ) {
Date date = new Date ( ) ;
long time= date. getTime ( ) ;
System. out. println ( date) ;
}
}
import java. security. Signature;
import java. text. ParseException;
import java. text. SimpleDateFormat;
import java. util. Date;
import java. util. Scanner;
public class Dem0date {
public static void main ( String[ ] args) throws ParseException {
Scanner sc= new Scanner ( System. in) ;
System. out. println ( "请输入您的出生日期 格式为yyyy-MM-dd" ) ;
String birthdaydateString= sc. next ( ) ;
SimpleDateFormat sdf= new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Date birthdayDate = sdf. parse ( birthdaydateString) ;
long birthdayDateTime = birthdayDate. getTime ( ) ;
long todayTime= new Date ( ) . getTime ( ) ;
long time= birthdayDateTime- todayTime;
System. out. println ( time/ 1000 / 60 / 60 / 24 ) ;
}
}
计算出生时间
import java. security. Signature;
import java. text. ParseException;
import java. text. SimpleDateFormat;
import java. util. Date;
import java. util. Scanner;
public class Dem0date {
public static void main ( String[ ] args) throws ParseException {
Scanner sc= new Scanner ( System. in) ;
System. out. println ( "请输入您的出生日期 格式为yyyy-MM-dd" ) ;
String birthdaydateString= sc. next ( ) ;
SimpleDateFormat sdf= new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Date birthdayDate = sdf. parse ( birthdaydateString) ;
long birthdayDateTime = birthdayDate. getTime ( ) ;
long todayTime= new Date ( ) . getTime ( ) ;
long time= todayTime- birthdayDateTime;
System. out. println ( time/ 1000 / 60 / 60 / 24 ) ;
}
}