First, we will use an example to briefly review the date-and time-related classes in Java:
Package day7; import java. text. simpleDateFormat; import java. util. date;/*** Date class: processing Date, time. Most of the methods in this class are out of Date. We do not recommend that you use * from JDK 1.1, use the Calendar class to convert the Date and time fields * use the DateFormat class to format and parse the Date string * Common constructor: assign a Date object to Date () and initialize this object, to indicate the time (accurate to milliseconds) for allocating it ** method not outdated: * boolean after (Date when) test whether the Date is * boolean before (Date when) after the specified Date) test whether the date is before the specified date */public class DateDemo {public static void main (String [] args) {// from 1970 The number of milliseconds since on January 1, January 1. out. println (System. currentTimeMillis (); // 1368598309172 Date date = new Date (); System. out. println (date); // Wed May 15 14:13:08 CST 2013/** but this date format does not meet our usage habits. How to display it as 14:13:08 Monday * We can use java. text. simpleDateFormat is used to format a date * SimpleDateFormat is a specific class for formatting and parsing a date in a language environment * It allows formatting (date-> text), parsing (text-> date) and normalization */SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-M-d HH: Mm: ss E "); // format the date format in the specified format as a string // date --> stringString dd = sdf. format (date); System. out. println (dd); // 14:19:53 Wednesday/** parse Date String: String time --> Date * Date parse (String text) parse the text of the String to generate Date. */}}
Exercise questions: Write two methods
1) output the specified String type time in a certain time format (using format)
2) convert the String time in a certain time format to the Date type time (Using parse)
Package day7; import java. text. parseException; import java. text. simpleDateFormat; import java. util. date; public class DateTestDemo {public static void main (String [] args) {Date date = new Date (); String str = dateToString (date); System. out. println (str); // 2013-05-15 14:34:50 wednesstring str1 = "2013-05-20"; Date date2 = stringToDate (str1); System. out. println (date2 );} /*** output the specified String type time in the format of "12:00:20 Monday" * @ param date passed into the Date object * @ return returns the converted String */public static String dateToString (Date date) {SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss E"); String str = sdf. format (date); return str;} public static Date stringToDate (String str) {SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); Date date = null; try {date = sdf. parse (str); return date;} catch (ParseException e) {e. printStackTrace ();} return null ;}}
Common date mode letters:
Y: Year
M: Month
D: days in the month
D: days in the year
E: days in a week
H: hour
M: minute
S: seconds