1 package com.dhb.code; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Calendar; 6 import java.util.Date; 7 8 /* * 9 * @author DSHORE /2018-5-2 10 * 11 */ 12 /* 13 * date(class): Date 14 * 15 * calendar(class): Calendar 16 * 17 * date format(class): SimpleDateFormat 18 * */ 19 public class Demo7 { 20 public static void main(String[] args) throws ParseException { 21 Date date = new Date(); // Create a Date object and get the current system time 22 System.out .println ( " Year: " +date. getYear()); // Obsolete 23 24 Calendar calendar = Calendar.getInstance(); 25 System.out .println ( " Year: " +calendar.get ( Calendar.YEAR)); // Return value: 2018 26System.out .println ( " Month: " +(calendar.get (Calendar.MONTH)+ 1 )); // Return value: 5 27 System.out .println ( " Day: " + calendar.get ( Calendar.DATE )); // Return value: 2 28 System.out .println ( " Hour: " +calendar.get ( Calendar.HOUR)); // Return value: 3 // 12-hour system 29 System.out .println ( " time: " +calendar.get (Calendar.HOUR_OF_DAY)); // Return value: 15 // 24-hour system 30 System.out .println ( " Minutes : " +calendar.get ( Calendar.MINUTE)); // Return value: 24 31 System.out .println ( " Seconds: " +calendar.get ( Calendar.SECOND)); // Return value: 40 32 System.out .println ( " Milliseconds: " + calendar.get ( Calendar.MILLISECOND)); // Return value: 119 1000 milliseconds = 1 second 33 34 // Display current system time: August 13, 2017 xx hours xx minutes xx seconds 35 // Date format class: SimpleDateFormat 36 Date date2 = new Date(); // Get Current system time 37 System.out .println (date2); // Return value: Wed May 02 09:24:40 CST 2018 38 SimpleDateFormat dateFormat = new SimpleDateFormat( " yyyy year MM month dd HH:mm:ss " ); 39 String time = dateFormat.format(date); 40 System.out .println ( "The current system time: " +time); // Return value: current system time: May 02, 2018 09:24:40 41 42 String birthday = " December 26, 2000 11:29:30 " ; 43 Date date3 = dateFormat.parse (birthday); // Note: The specified string format must be consistent with the format of SimpleDateFormat. 44 System.out .println ( date3 ); // Return value: Tue Dec 26 11:29:30 CST 2000 45 46 Date date4 = new Date(); 47 SimpleDateFormat dateFormat2 = new SimpleDateFormat( " MM, MM, DD, yyyy HH:mm:ss " ); 48 String time1 = dateFormat2.format(date4); 49 System.out .println ( " Current system time: " +time1); // Return value: Current system time: May 02, 2018 09:24:40 50 51 String birthday1 = " December 11, 2001 12:11:38 " ; 52 Date d = dateFormat2.parse(birthday1); 53 System.out .println (d); // Return value: Tue Dec 11 12:11:38 CST 2001 54 } 55 }
Original Author: DSHORE From: http://www.cnblogs.com/dshore123/ Welcome to reprint, reprint must indicate the source. ( If this article is useful to you, you can click Recommend , thank you! ) |