• Java Date, Calendar and Time API


     

    Java Date and Time API

    This article explains the API for using Calendar, Date and Time in Java and how to format the output of a date.

     

    1. Overview

    The Java language provides direct support for time-based objects. This article gives a few examples how this API can be used.

    The java.util.Date and the java.util.Calendar classes provide access to storing and manipulating dates.

    It is recommended to use Calendar if possible. Existing API may required that you convert from Date to Calendar and vice versa.

     

    2. Format date

    To format a date, you can use the SimpleDateFormat class. The following snippet gives several example for its usage.

    // use dd/MM/yy as format
    
    DateFormat df1 = new SimpleDateFormat("dd/MM/yy");
    String formattedDate1 = df1.format(new Date());
    
    // or use yyyy/MM/dd as format
    
    DateFormat df2 = new SimpleDateFormat("yyyy/MM/dd");
    String formattedDate2 = df2.format(theDate); 
     
    

      

    3. Working with Dates and Calendars

    3.1. Calendar

    The java.util.Calendar class is an abstract encapsulation of the Date object.

    Calendar provides getter and setter for the date fields.

    public final int get(int field)
    public final void set(int field, int value) 
     
    

      

    Table 1. Calendar field access

    Field	Explanation
    Calendar.YEAR	Identifies the year
    Calendar.MONTH	Identifies the month
    Calendar.DAY_OF_MONTH	Identifies the day
    Calendar.HOUR	Identifies the hour
    Calendar.MINUTE	Identifies the minute
    Calendar.SECOND	Identifies the second
     
    

      

    Tip

     The Calendar.MONTH starts with 0. So December is 11.

    Create a new Java project called JavaIntroCalendar. Create the following class for testing.

     
    
    package test;
    
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    
    public class CalendarTest {
      public static void main(String[] args) {
        // constructor allows to set year, month and date
        Calendar cal1 = new GregorianCalendar(2008, 01, 01);
        // constructor could also be empty
        // calendar cal2 = new GregorianCalendar();
        // change the month
        cal1.set(Calendar.MONTH, Calendar.MAY);
    
        System.out.println("Year: " + cal1.get(Calendar.YEAR));
        System.out.println("Month: " + (cal1.get(Calendar.MONTH) + 1));
        System.out.println("Days: " + cal1.get(Calendar.DAY_OF_MONTH));
    
        // format the output with leading zeros for days and month
        SimpleDateFormat date_format = new SimpleDateFormat("yyyyMMdd");
        System.out.println(date_format.format(cal1.getTime()));
    
      }
    } 
     
    

      

    3.2. Date and Date Conversion

    Use the following commands to convert to a Date from various formats.

    package conversion;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    
    public class ConversionExamplesDate {
    
      // convert from String to date
      private void stringToDate() {
        
        try {
          Date date1;
          date1 = new SimpleDateFormat("MM/dd/yy").parse("05/18/05");
          System.out.println(date1);
          Date date2 = new SimpleDateFormat("MM/dd/yyyy").parse("05/18/2007");
          System.out.println(date2);
        } catch (ParseException e) {
          e.printStackTrace();
        }
      }
    
      // convert from millisecs to a String with a defined format
      private void calcDate(long millisecs) {
        SimpleDateFormat date_format = new SimpleDateFormat("MMM dd,yyyy HH:mm");
        Date resultdate = new Date(millisecs);
        System.out.println(date_format.format(resultdate));
      }
      
      private void writeActualDate(){
        Calendar cal = new GregorianCalendar();
        Date creationDate = cal.getTime();
        SimpleDateFormat date_format = new SimpleDateFormat("MMM dd,yyyy HH:mm");
        System.out.println(date_format.format(creationDate));
      }
      
    
      public static void main(String[] args) {
        ConversionExamplesDate convert = new ConversionExamplesDate();
        convert.stringToDate();
        convert.calcDate(System.currentTimeMillis());
        convert.writeActualDate();
      }
    } 
    

      

  • 相关阅读:
    Compiling Open Source Software for UNIX using Configure Script
    vlcandroid 移植live555到android
    xcode中armv6与armv7的困惑
    ZOJ 3204 Connect them (最小生成树,输出字典序最小的解)
    POJ 3133 Manhattan Wiring (插头DP)
    HDU 4419 Colourful Rectangle 第37届ACM/ICPC 杭州赛区网络赛 1010题 (线段树)
    HDU 3829 Cat VS Dog (二分匹配求最大独立集)
    最大流模板(SAP算法)(邻接表形式)
    HDU 4417 Super Mario 第37届ACM/ICPC 杭州赛区网络赛第1008题 (划分树)
    ZOJ 3203 Light Bulb (数学直接推公式 或者 三分法)
  • 原文地址:https://www.cnblogs.com/hephec/p/4580018.html
Copyright © 2020-2023  润新知