java进阶 JDK7 -日期 -案例48
![](https://img2023.cnblogs.com/blog/2140850/202303/2140850-20230318160513810-2038425532.png)
package com.cyjt97.dt;
import java.util.Date;
public class day {
public static void main(String[] args) {
Date dt = new Date();
Date dt2 = new Date(0L);
System.out.println(dt);//Sat Mar 18 16:09:15 CST 2023
System.out.println(dt2);//Thu Jan 01 08:00:00 CST 1970
System.out.println(dt.getTime());//1679127078299
System.out.println(dt2.getTime());//0
}
}
package com.cyjt97.dt; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class day2 { public static void main(String[] args) throws ParseException { // 创建日期格式化对象,封装时间 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(); // 手动修改 SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); // 创建date对象,封装时间 Date date = new Date(); // 将日期时间转为字符串 String res =simpleDateFormat.format(date); String res2 =simpleDateFormat2.format(date); System.out.println(res);//2023/3/18 下午4:58 System.out.println(res2);//2023年03月18日 17:41:01 String today = "2023年3月18日"; SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy年MM月dd日"); Date date2 = simpleDateFormat1.parse(today); System.out.println(date2);//Sat Mar 18 00:00:00 CST 2023 } }
package com.cyjt97.dt; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class day3 { public static void main(String[] args) throws ParseException { Scanner sc = new Scanner(System.in); System.out.println("请输入您的生日"); String b = sc.nextLine(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日"); Date bb = simpleDateFormat.parse(b); Date TODAY = new Date(); long time = TODAY.getTime()-bb.getTime(); System.out.println(time/1000/60/60/24); } }
package com.cyjt97.dt;
import java.util.Calendar;
import static java.util.Calendar.*;
public class day4 {
public static void main(String[] args) {
// Calendar c是抽象方法
// Calendar.getInstance()是子类对象
Calendar c = getInstance();
// 年
int i = c.get(YEAR);
// 月
int i2 = c.get(MONTH);
// 日
int i3 = c.get(DAY_OF_MONTH);
// 周
int week = c.get(DAY_OF_WEEK);
char weekIndex[] = {' ', '日', '一', '二', '三', '四', '五', '六'};
System.out.println(i);
System.out.println(i2 + 1);
System.out.println(i3);
System.out.println(weekIndex[week]);
// 修改年份信息
c.set(YEAR,2022);
System.out.println(c.get(YEAR));
// 修改年份信息
c.set(2008,12,16);
System.out.println(c.get(YEAR)+'年');
System.out.println(c.get(Calendar.MONTH)+'月');
System.out.println(c.get(DAY_OF_WEEK)+'日');
// 对年份做增减
c.add(YEAR,-1);
}
}
package com.cyjt97.dt; import java.util.Calendar; import static java.util.Calendar.DAY_OF_WEEK; public class day5 { public static void main(String[] args) { Calendar c = Calendar.getInstance(); c.set(2023, 2, 23); char weekIndex[] = {' ', '日', '一', '二', '三', '四', '五', '六'}; int week = c.get(DAY_OF_WEEK); if (weekIndex[week] == '四'){ System.out.println("疯狂星期四"); }else { System.out.println("not"); } } }
package com.cyjt97.dt; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Scanner; public class day6 { public static void main(String[] args) throws ParseException { Scanner sc = new Scanner(System.in); System.out.println("请输入"); String a = sc.nextLine(); SimpleDateFormat dt = new SimpleDateFormat("yyyy年MM月dd日"); Date date = dt.parse(a); Calendar c = Calendar.getInstance(); c.setTime(date); int day = c.get(Calendar.DAY_OF_YEAR); System.out.println(day); } }
代码改变了我们,也改变了世界