java.lang:不需要显示导入
包装类
String类:
StringBuffer类:
Math类:
Object:
java.util:java工具包
操作时间和日期、集合
Date类:表示时间和日期
时间和日期:年月日时分秒毫秒星期
Tue Mar 14 08:53:43 CST 2017 +8:00
Date d = new Date(year,month,date);
Calendar类:日历
SimpleDateFormat类:格式化时间和日期的类
案例:计算合同的续签日
规则:合同到期的前一个月续签合同,如果是周末
则顺延至下一个星期一
输入参数:合同签订时间,合同签订年限
输出参数:返回下一次合同的续签日
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class TestNextSign { public static Date nextSign(Date sign, int year) { Calendar cal = Calendar.getInstance(); cal.setTime(sign); cal.add(Calendar.YEAR, year); cal.add(Calendar.MONTH, -1); if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { cal.add(Calendar.DATE, 1); } if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) { cal.add(Calendar.DATE, 2); } return cal.getTime(); } public static void main(String[] args) { // 合同的签订时间 String str = "2015-9-12"; // 合同签订年限 int year = 2; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { Date sign = sdf.parse(str); Date nextDate = nextSign(sign, year); System.out.println(sdf.format(nextDate)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
作业:
1、输入年份,打印该年的日历
import java.util.Calendar; import java.util.Scanner; public class MyCalendar { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入年份:"); int year = sc.nextInt(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); for (int j = 1; j <= 12; j++) { cal.set(Calendar.MONTH, j - 1); cal.set(Calendar.DATE, 1); //取得当前月的最大的天数 int maxday = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //// 一号是第一个星期的第几天 int weekday = cal.get(Calendar.DAY_OF_WEEK); System.out.println(" " + year + "年" + j + "月的日历"); System.out.println("日 一 二 三 四 五 六"); for (int i = 1; i < weekday; i++) { System.out.print(" "); } for (int i = 1; i <= maxday; i++) { System.out.print(i + " "); if ((weekday - 1 + i) % 7 == 0) System.out.println(); } System.out.println(); } } }
2、计算商品的促销时间
规则:商品到期的前一个周五开始促销
输入:商品的生产日期和保质期(月)
输出:商品的促销日
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Scanner; public class SalesCalendar { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入生产日期:"); String date = sc.next(); System.out.println("请输入保质期:"); int month = sc.nextInt(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { Date produceDate = sdf.parse(date); Date salesDate = getsalesDate(produceDate, month); System.out.println(sdf.format(salesDate)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static Date getsalesDate(Date produceDate, int month) { Calendar cal = Calendar.getInstance(); cal.setTime(produceDate); cal.add(Calendar.MONTH, month); cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_WEEK)-8); return cal.getTime(); } }
3、计算自己活了多少天和多少毫秒?
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class LifeAlive { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入出生日期"); String birthDate = sc.next(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date sign; try { sign = sdf.parse(birthDate); Date date = new Date(); long i = sign.getTime(); long j = date.getTime(); System.out.println(j - i); System.out.println((j - i) / (1000 * 60 * 60 * 24)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }