Class 1.Date
Date represents a specific time, accurate to milliseconds
public Date() ***** assigns a Date object and initializes it so that it represents the time it was assigned, accurate to milliseconds
public Date(long date) ***** assigns a Date object and initializes it to represent the number of milliseconds specified from the standard base time
public class DateDemo01 { public static void main(String[] args) { //public Date(): assign a Date object and initialize it so that it represents the time it was assigned, accurate to milliseconds Date d1 = new Date(); System.out.println(d1); //public Date(long date): assigns a Date object and initializes it to represent the number of milliseconds specified from the standard base time long date = 1000*60*60; Date d2 = new Date(date); System.out.println(d2); } }
2... Common methods of Date class
public long getTime() * * * gets the milliseconds value of the date object from 00:00:00, January 1, 1970 to now
public void setTime(long time) * * set the time to the value of milliseconds
public class DateDemo02 { public static void main(String[] args) { //Create date object Date d = new Date(); //public long getTime(): get the milliseconds value of the date object from 00:00:00, January 1, 1970 to now // System.out.println(d.getTime()); // System.out.println(d.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "year"); //public void setTime(long time): set the time to the value of milliseconds // long time = 1000*60*60; long time = System.currentTimeMillis(); d.setTime(time); System.out.println(d); } }
3... SimpleDateFormat class
Public SimpleDateFormat() * * * constructs a SimpleDateFormat, using the default mode and date format
Public SimpleDateFormat (string pattern) * * construct a SimpleDateFormat using the given pattern and the default date
Common methods of SimpleDateFormat class:
- Format (from Date to String)
- public final String format(Date date): format date into date / time string
- Parsing (from String to Date)
- public Date parse(String source): parse text from the beginning of a given string to generate a date
public class SimpleDateFormatDemo { public static void main(String[] args) throws ParseException { //Format: from Date to String Date d = new Date(); // SimpleDateFormat sdf = new SimpleDateFormat(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy year MM month dd day HH:mm:ss"); String s = sdf.format(d); System.out.println(s); System.out.println("--------"); //From String to Date String ss = "2048-08-09 11:11:11"; //ParseException SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dd = sdf2.parse(ss); System.out.println(dd); } }
4 date tool cases
Define a date utility class, which contains two methods: convert date to a string of specified format; parse string to a date of specified format, and then define a test class (DateDemo), which is a method to test date utility class
Tools: public class DateUtils { private DateUtils() {} Converts a date to a string of the specified format Return value type: String Parameters: Date date, String format public static String dateToString(Date date, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); String s = sdf.format(date); return s; } Date to parse string to specified format Return value type: Date Parameters: String s, String format public static Date stringToDate(String s, String format) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat(format); Date d = sdf.parse(s); return d; } }
Test class public class DateDemo { public static void main(String[] args) throws ParseException { //Create date object Date d = new Date(); String s1 = DateUtils.dateToString(d, "yyyy year MM month dd day HH:mm:ss"); System.out.println(s1); String s2 = DateUtils.dateToString(d, "yyyy year MM month dd day"); System.out.println(s2); String s3 = DateUtils.dateToString(d, "HH:mm:ss"); System.out.println(s3); System.out.println("--------"); String s = "2048-08-09 12:12:12"; Date dd = DateUtils.stringToDate(s, "yyyy-MM-dd HH:mm:ss"); System.out.println(dd); } }
Class 5Calendar
Public int get (int field) returns the value of the given calendar field
Public abstract void add (int field, int amount) according to the rules of the calendar, add or subtract the given calendar field from the specified amount of time
public final void set(int year,int month,int date)
// get(int field): public class CalendarDemo { public static void main(String[] args) { //Get calendar class object Calendar c = Calendar.getInstance(); //public int get(int field): returns the value of the given calendar field int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH) + 1; int date = c.get(Calendar.DATE); System.out.println(year + "year" + month + "month" + date + "day"); } }
//set(int year,int month,int date) public class CalendarDemo { public static void main(String[] args) { //Get calendar class object Calendar c = Calendar.getInstance(); c.set(2050,10,10); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH) + 1; date = c.get(Calendar.DATE); System.out.println(year + "year" + month + "month" + date + "day"); } }
// add(int field, int amount) public class CalendarDemo { public static void main(String[] args) { //Get calendar class object Calendar c = Calendar.getInstance(); //Demand 2: 10 days after 10 years c.add(Calendar.YEAR,10); c.add(Calendar.DATE,-10); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH) + 1; date = c.get(Calendar.DATE); System.out.println(year + "year" + month + "month" + date + "day"); } }
6... February day case
- Get the number of days in February of any year
- code implementation
public class CalendarTest { public static void main(String[] args) { //Enter any year by keyboard Scanner sc = new Scanner(System.in); System.out.println("Please enter year:"); int year = sc.nextInt(); //Set the year, month and day of the calendar object Calendar c = Calendar.getInstance(); c.set(year, 2, 1); //March 1 is the last day of February c.add(Calendar.DATE, -1); //Get the output of the day int date = c.get(Calendar.DATE); System.out.println(year + "February of" + date + "day"); } }
7 exceptions
public class ExceptionDemo { public static void main(String[] args) { System.out.println("start"); // method(); try { method2(); }catch (ParseException e) { e.printStackTrace(); } System.out.println("End"); } ----------------------------------- //Compile time exception public static void method2() throws ParseException { String s = "2048-08-09"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date d = sdf.parse(s); System.out.println(d); } ------------------------------------- //Runtime exception public static void method() throws ArrayIndexOutOfBoundsException { int[] arr = {1, 2, 3}; System.out.println(arr[3]); } }