Java: Getting started from scratch, getting started with learning notes & lt; Date class _ Calendar Class & gt; (17th)

Source: Internet
Author: User
Tags date1

The Date class _ Calendar class Java. util. Date class is an encapsulation class. If you create an object of this encapsulation class, you can get the system time, and the system time obtained is a Long data type. The data of this long type is created from a millisecond difference between the standard GMT Standard Time and the system time. The object of a Date class is the same as that of a common class. Date = new date (); package com. ibm. utildate; import java. util. date; public class DateDemo {public static void main (String [] args) {// Date object creation Date date = new Date (); System. out. println (date); Date date1 = new Date (112,8, 8); System. out. println (date1); // after and before Date date3 = new Date (114,9, 9); boolean flag = date. after (date3); Syst Em. out. println (flag); // returns a millisecond value after obtaining the system time. long l = date. getTime (); System. out. println (l); // How many days are there between 1970 and? Long l1 = l/1000/60/60/24; System. out. println (l1); // java. SQL. date java. SQL. time java. SQL. timeatamp is java. util. subclass of the Date class // convert java. util. date and java. SQL. date mutual conversion java. util. date sd = new java. SQL. date (11); java. util. date ud = sd; // if you want. util. convert Date to java. SQL. date. SQL. convert Date to java. util. date // is the rule of mutual conversion between child classes and parent classes} How many days have you been born? Package com. ibm. utildate; import java. util. calendar; import java. util. gregorianCalendar; public class GetDayDemo {// how many days have you been born public static void main (String [] args) {// create a Calenbdar object Calendar cl = new GregorianCalendar (); // 1990.9.9 cl. set (1990,); // obtain the number of milliseconds from 90 years to the present. long cals = cl. getTimeInMillis (); long now = Calendar. getInstance (). getTimeInMillis (); long days = now-cals; System. out. prin Tln (days); // converts the number of milliseconds to a day int n = (int) (days/(1000*60*60*24); System. out. println ("born after 90" + n + "days"); System. out. println ("born after 90" + n/365 + "years") ;}} SimpleDateFormat: package com. ibm. utildate; import java. text. simpleDateFormat; import java. util. date; import java. text. *; public class SimpleDateFormatDemo {public static void main (String [] args) throws ParseException {String birth = "12:24:33 "; SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); // Date d = new Date (); // String str = sdf. format (d); // System. out. println (str); // convert a string to the Date type // Date date = sdf. parse (birth); // System. out. println (date); // System. out. println (new Date (); // DateFormat df = DateFormat. getDateInstance (); String stres = df. format (new Date (); System. out. println (stres); // DateFormat df1 = DateForm At. getDateInstance (DateFormat. LONG); String str1 = df1.format (new Date (); System. out. println (str1); // returns the instance DateFormat df2 = DateFormat at a time. getTimeInstance (DateFormat. FULL); String str2 = df2.format (new Date (); System. out. println (str2) ;}} Calendar: package com. ibm. utildate; import java. io. objectInputStream. getField; import java. util. calendar; import java. util. date; import java. util. gregorianCalend Ar; public class CalendarDemo {public static void main (String [] args) {// canlendardemo can be used to obtain part of the data in a time period, for example, minute // calendar is the parent class of GregorianCalendar // calendar Ar is an abstract class // 1 the instance for creating a Calendar cl = new GregorianCalendar (); // Calendar cl1 = Calendar. getInstance (); // System. out. println (cl1); // System. out. println (cl = cl1); // 2 use the calendar class to obtain the calendar and time // Date date = cl. getTime (); // System. out. println (date );//// Get the first day of a week // int fday = cl. getFirstDayOfWeek (); // System. out. println (fday); // obtain the day of the week from which the current date is obtained. // int dow = cl. get (Calendar. DAY_OF_WEEK); // System. out. println (dow); ///// obtain the number of months in the current month. // int my = cl. get (Calendar. MONTH); // System. out. println (my); ////// maximum number of days determined by month // int maxday = cl. getActualMaximum (Calendar. DAY_OF_MONTH); // System. out. println (maxday); ////// when is the week of the month obtained? Int wom = cl. get (Calendar. WEEK_OF_MONTH); System. out. println (wom); // obtain the month int month = Calendar. MONTH; // The maximum number of days of the MONTH. int maxday = Calendar. DAY_OF_MONTH ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.