package zuoye;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Scanner;
public class lianxi_2 {
public static void main(String[] args) {
}
@Test
// 1. 请使用日期时间相关的API,计算出一个人已经出生了多少年,
// 再转换为多少天(一年365天)。
public void tast1() throws ParseException {
Scanner sc=new Scanner(System.in);
System.out.println("请输入您的生日:(2022-12-12)");
String birthdayStr=sc.next();
System.out.println(birthdayStr);
SimpleDateFormat sdf=new SimpleDateFormat("yyy-MM-dd");
Date birthday=sdf.parse(birthdayStr);
System.out.println(birthday);
Calendar c=Calendar.getInstance();
c.setTime(birthday);
int year=c.get(Calendar.YEAR);
//获取当前时间的年份
Calendar c2=Calendar.getInstance();
int year2=c2.get(Calendar.YEAR);
Date now=new Date();
System.out.println("您已经出生了"+(year2-year)+"年");
int days=(int) ((now.getTime()-birthday.getTime())/(1000*50*60*24));
System.out.println("您出生了"+days+"天");
}
@Test
public void tast1_2(){
Scanner sc=new Scanner(System.in);
System.out.println("请输入您的生日:(2022-12-12)");
String birthdayStr=sc.next();
DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate birthdayDate = LocalDate.parse(birthdayStr, dtf);
int birthYear = birthdayDate.get(ChronoField.YEAR);
System.out.println(birthYear);
LocalDate now=LocalDate.now();
System.out.println("您活了"+(now.get(ChronoField.YEAR)-birthYear)+"年");
long nowDay=now.toEpochDay();
long days=nowDay-birthdayDate.toEpochDay();
System.out.println();
}
@Test
// 2. 获取当前的日期,并把这个日期转换为指定格式的字符串
// 如2088-08-08 08:08:08
public void tast2(){
Date now=new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format=sdf.format(now);
System.out.println(format);
LocalDateTime time=LocalDateTime.now();
DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String timeStr=time.format(dtf);
System.out.println(timeStr);
}
@Test
//3. 判断2018年2月14日是星期几
public void tast3(){
Calendar c=Calendar.getInstance();
c.set(2018,1,18);
c.get(Calendar.WEEK_OF_MONTH);
c.get(Calendar.WEEK_OF_YEAR);
int week1=c.get(Calendar.WEEK_OF_MONTH);
System.out.println(week1);
int i=c.get(Calendar.DAY_OF_WEEK);
System.out.println(i);
}
@Test
public void tast3_2(){
LocalDate date = LocalDate.of(2018, 2, 3);
int w = date.get(ChronoField.DAY_OF_WEEK);
System.out.println(w);
}
}