package cd_one.code14.exer2;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTimeTest {
@Test
public void test(){
Date date = new Date();
System.out.println(date);
System.out.println("对应的毫秒数:"+date.getTime());
Date date2 = new Date(1720303134781l);
System.out.println(date2);
}
@Test
public void test2(){
java.sql.Date date1 = new java.sql.Date(1720303134781l);
System.out.println(date1);//2024-07-07
System.out.println(date1.getTime());
}
@Test
public void test3() throws ParseException {
// SimpleDateFormat类,用于时间日期的格式化和解析
SimpleDateFormat sdf = new SimpleDateFormat();
//格式化:日期--->字符串
Date date1 = new Date();
String strDate = sdf.format(date1);
System.out.println(strDate);//2024/3/13 下午1:45
//字符串--->日期
Date date2 = sdf.parse("2024/3/13 下午1:45");
System.out.println(date2);//Wed Mar 13 13:45:00 CST 2024
}
}
————————————————————
package cd_one.code14.exer3;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
*
* 将控制台获取的年月日(比如:2022-12-13)的字符串数据,保存在数据库中
*/
public class DateTimeTest {
@Test
public void test2(){
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getClass());
}
@Test
public void test1(){
Calendar calendar = Calendar.getInstance();
System.out.println(Calendar.DAY_OF_MONTH);//5
System.out.println(Calendar.DAY_OF_YEAR);//6
calendar.set(Calendar.DAY_OF_MONTH,23);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//23
//
calendar.add(Calendar.DAY_OF_MONTH,3);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//26
calendar.add(Calendar.DAY_OF_MONTH,-5);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//21
Date date = calendar.getTime();//21
System.out.println(date);//Thu Mar 21 19:33:11 CST 2024
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//21
}
@Test
public void test3() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String pattern = "2022-12-13";
Date date1 = sdf.parse(pattern);
java.sql.Date date2 = new java.sql.Date(date1.getTime());
System.out.println(date2);//2022-12-13
}
@Test
public void test4(){
System.out.println(LocalDate.now());//2024-03-13
System.out.println(LocalDate.now());//2024-03-13
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);//2024-03-13T19:42:17.955574800
System.out.println(LocalDate.of(2024, 3, 13));//2024-03-13
LocalDateTime localDateTime1 = LocalDateTime.of(2022,11,5,11,23,45);//
System.out.println(localDateTime1);//2022-11-05T11:23:45
LocalDateTime localDateTime2 = LocalDateTime.now();
System.out.println(localDateTime2.getDayOfMonth());//13
LocalDateTime localDateTime3 = localDateTime2.withDayOfMonth(15);
System.out.println(localDateTime3);//2024-03-15T19:48:56.394719400
}
@Test
public void test5(){
Instant instant = Instant.now();
System.out.println(instant);//2024-03-13T11:53:41.533527600Z
OffsetDateTime instant1 = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(instant1);//2024-03-13T19:53:41.533527600+08:00
Instant instant2 = Instant.ofEpochMilli(234325544L);
System.out.println(instant2);//1970-01-03T17:05:25.544Z
long millTime = instant.toEpochMilli();
System.out.println(millTime);//1710330821533
System.out.println(new Date().getTime());//1710330821544
}
}
package cd_one.code14.exer3;
import cd_one.code6.Exer03.CircleTest;
import org.junit.Test;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
/**
*
* 使用Calendar获取当前时间,把这个时间设置为你的生日,在获取你的百天(出生后100天)日期。
*/
public class Exer2 {
@Test
public void test(){
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
System.out.println("你的生日为:" + date);
calendar.add(Calendar.DAY_OF_YEAR,100);
Date newDate = calendar.getTime();
System.out.println("100天以后是:" + newDate);
//你的生日为:Wed Mar 13 20:01:38 CST 2024
//100天以后是:Fri Jun 21 20:01:38 CST 2024
}
@Test
public void test2(){
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("你的生日为:"+ localDateTime);
LocalDateTime localDateTime1 = localDateTime.plusDays(100);
System.out.println("100天以后是:" + localDateTime1);
// 你的生日为:2024-03-13T20:04:11.280317600
// 100天以后是:2024-06-21T20:04:11.280317600
}
}