Episode 67: Usage/Date Computation of Common Classes/Calendar and Gregorian Calendar
The Calendar class is an abstract class that provides methods for translating a particular instant into a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and for manipulating calendar fields such as getting the date of next week.
The moment can be expressed in milliseconds, which is the offset from the epoch (i.e. Greenwich Standard Time, January 1, 1970, 00:00:00.000, Gregorian Calendar).
Gregorian Calendar is a specific subclass of Calendar that provides standard calendar systems used in most countries/regions of the world.
Note the month's indication, January is 0, February is 1, and so on, December is 11. Because most people are used to using words instead of numbers to represent months, programs may be easier to read. The parent Calendar uses constants to represent months: JANUARY, FEBRUARY, and so on.
Pay attention to the expression of the week, Sunday is 1, Monday is 2, and so on.
package com.test067; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class Test067 { public static void main(String[] args){ Calendar a = new GregorianCalendar(); //Setting Date a.set(2007, 0, 2, 3, 4, 5);//Use the set method of the Gregorian Calendar class to set the time at one time. Parameters that are not set default to the current time System.out.println("Print one GregorianCalendar class:\t"+a); a.set(2007,Calendar.JANUARY,2,3,4,5);//This parameter is set with Calendar.JANUARY System.out.println("Print one GregorianCalendar Class:\t"+a); a.set(Calendar.YEAR, 2013);//Setting a single parameter System.out.println("Printing Calendar Class YEAR Option values:\t"+a.get(Calendar.YEAR)); System.out.println("Printing Calendar class YEAR Default values for constants:\t"+Calendar.YEAR);// //Date of acquisition int year = a.get(Calendar.YEAR); System.out.println("Output int Type year"+year); //Calendar.Date is synonymous with Calendar.DAY_OF_MONTH. //Date calculation a.add(Calendar.YEAR,30); System.out.println("Printing increases 30 years later:\t"+a.get(Calendar.YEAR)); a.add(Calendar.MONTH, 30);//12 months is a cycle System.out.println("Months after printing increases by 30 months:\t"+a.get(Calendar.MONTH)); //Conversion from Date Object to Time Object Calendar c = new GregorianCalendar(); Date b = c.getTime();//Return a Date object using the getTime method of the GregorianCalendar class System.out.println("Print one Date Class:\t"+b); //Conversion from Time Object to Date Object Date d = new Date(); Calendar e = new GregorianCalendar(); e.setTime(d); System.out.println("Print one by Date Converted from the object of the class GregorianCalendar Class object:\t"+e); //Conversion of Date Object to long Type long millSecond = c.getTimeInMillis();//Returns the millisecond representation of this Calendar object System.out.println("Print one long Type:\t"+millSecond); long g = System.currentTimeMillis(); System.out.println("Print the current time long Type representation:\t"+g); } }
Collection 68-69: Visualization of Calendar Procedures for Common Classes
Today, this lesson has a great impact on me, a project that I have no idea at all. Teacher Gao guided us to take action and improve little by little, and did it slowly! So the most efficient way to do anything is never to think first and then act. It's the most efficient way to think while acting. Of course, it's certainly not the most comfortable way.
/** * Summary of ideas: * 1.The first step is to build the skeleton through concrete actions. * 2.According to the meaning of the question, using calendar knowledge to solve practical problems, three knowledge points * a)The first day of this month is the Wednesday, which determines the number of spaces ahead. int: w=Calendar.get(Calendar.Date) * * b)Whether the last day of the month is 30 or 31 or 28 determines the critical value of the cycle. c) The input string passes through new * simpleDateFormat("yyyy-MM-dd").parse(str)Become a date object. There are Calendar c = new Gregorian Calendar (); and c. setTime (date) methods to turn date objects into calendars. */ package com.test068; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Scanner; public class Test068 { public static void main(String[] args){ System.out.println("Please enter the date (according to format: 2030)-3-10): "); Scanner scanner = new Scanner(System.in); String temp = scanner.nextLine(); DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = format.parse(temp); Calendar calendar = new GregorianCalendar(); calendar.setTime(date); int day = calendar.get(Calendar.DATE); calendar.set(Calendar.DATE,1); int maxDate = calendar.getActualMaximum(Calendar.DATE); System.out.println("day\t One\t Two\t Three\t Four\t Five\t Six"); for(int i = 1;i<calendar.get(Calendar.DAY_OF_WEEK);i++){ System.out.print("\t"); } for(int i =1;i<=maxDate;i++){ if(i==day){ System.out.print("*"); } System.out.print(i+"\t"); int w = calendar.get(Calendar.DAY_OF_WEEK); if(w==Calendar.SATURDAY){ System.out.println(); } calendar.add(Calendar.DATE,1); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Set 70: Use of Common Class _file
java.io.File class: An abstract representation of file and directory pathnames.
A common construction method for File classes: public File(String pathname) creates File objects with pathname as the path. If pathname is a relative path, the default current path is stored in the system attribute user.dir.
The static attribute String separator of File stores the path separator of the current system.
-
File objects allow access to the properties of files.
public boolean canWrite() public boolean exists() public boolean isDirectory() public boolean isFile() public boolean isHidden() public long lastModified() public long length() public String getName() public String getPath()
-
Create an empty file or directory through a File object (in the absence of the file or directory that the object refers to).
public boolean createNewFile()throws IOException public boolean delete() public boolean mkdir(), mkdirs()
Sample code
package com.test070; import java.io.File; import java.io.IOException; public class Test070 { public static void main(String[] args){ /** * File File Class: 1. Representation File 2. Representation Catalogue */ File a = new File("/Users/wangtao/Desktop/aaa.txt"); File a2 = new File("/Users/wangtao/Desktop/"); File a3 = new File(a2,"aaa.txt"); File a4 = new File(a2,"bbb.txt"); File a5 = new File("/Users/wangtao/Desktop/AAA/BBB/CCC"); File a6 = new File("/Users/wangtao/Desktop/AAA/DDDD"); try { a3.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(a.isFile()){ System.out.println("File!"); } if(a2.isDirectory()){ System.out.println("Directory"); } if(a3.isFile()){ System.out.println("File!"); } a5.mkdirs();//Direct creation a6.mkdir();//Do not create } }