The role of SimpleDateFormat
1: Convert a Date object to a string of a specific format method
2: Convert a string to a Date object in a specific format parse method
Package Day25;import Java.text.parseexception;import Java.text.simpledateformat;import Java.util.Date;public class Demo07 {public static void main (string[] args) throws parseexception{string timerstr = "2016-01-13 18:59:01"; SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");D ate Date = Format.parse (TIMERSTR); SYSTEM.OUT.PRINTLN (date);}}
DateFormat class
is the parent of SimpleDateFormat, you only need to invoke a static method provided by it when creating an instance getinstance (), do not use new This is the factory method used before
Package Day25;import Java.text.dateformat;import Java.util.date;import Java.util.locale;public class Demo08 {//Will Date object is converted to a specific format string public static void main (string[] args) {Date date = new Date ();D ateformat format = dateformat.getdateinstance (Dateformat.long,locale.china); System.out.println (Format.format (date));}}
Calendar class
The calculation of the date can be carried out conveniently.
Issues such as time zone are taken into account when getting information from a date
Creating an instance can often use the factory method provided
GetInstance () automatically creates an implementation class for the corresponding time zone based on the region in which the current system is located
Calendar makes it easy to set a time
Set (int field,int value);
Get some information about the time that the calendar describes
Get (int field);
The method of calculating the time adds the given value to the given time unit and causes the current calendar to represent this time
Add (int field,int value);
package day25;import java.text.simpledateformat;import java.util.calendar;import Java.util.date;import java.util.gregoriancalendar;public class demo09 {public static void main (String[] args) {//Create calendar instance calendar is abstract class has subclass created// The calendar instance that was created describes the current system time//Calculation time calendar calendar = calendar.getinstance (); SYSTEM.OUT.PRINTLN (calendar);//GMT//calendar = new gregoriancalendar ();//Intermediary Date date = calendar.gettime (); SYSTEM.OUT.PRINTLN (date);//output using Simpledateformat fmt = new simpledateformat (" Yyyy-mm-dd "); String str = fmt.format (date); System.out.println (str);//Set a point-in-time Calendar.set (calendar.year, 1111); Calendar.set (Calendar.month, Calendar.november); Calendar.set (calendar.day_of_month,11); System.out.println (Calendar.gettime ()); Calendar.set (calendar.day_of_month,1111); System.out.println (Calendar.gettime ()); Calendar.set (calendar.hour_of_DAY, 11); Calendar.set (calendar.minute, 11); Calendar.set (calendar.second, 11); System.out.println (Calendar.gettime ()); Int week = calendar.get (Calendar.day_of_week); SYSTEM.OUT.PRINTLN (week);//Foreign Sunday is the first day of Int day = calendar.get (calendar.day_of_year); System.out.println (day); Int sumofyear = calendar.getactualmaximum (calendar.day_of_year);// The maximum value of the incoming year int sumofmonth = calendar.getactualmaximum (calendar.day_of_month); System.out.println (sumofyear); System.out.println (sumofmonth);//calculation today is the day of the week (other companies do not know, our company is very commonly used to calculate Saturday and Sunday, do not open)//Calculate the Saturday and Sunday of the year to generate no open schedule (certain) Join the holiday manually (this is uncertain)}}
Package Day25;import Java.util.calendar;public class CalendarDemo10 {public static void main (string[] args) {Calendar Calendar = Calendar.getinstance (); Calendar.add (calendar.month,1); int week = Calendar.get (Calendar.day_of_week); SYSTEM.OUT.PRINTLN ("Week" + (week==1?7:week-1));}}
This article from the "Romantic Smile" blog, reproduced please contact the author!
Java25:simpledateformat;dateformat