Java Date, Calendar and Time api-tutorial

Source: Internet
Author: User
Tags date1

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.

Table of Contents

1. Overview
2. Format Date
3. Working with Dates and calendars
3.1. Calendar
3.2. Date and date Conversion
4. About this website
4.1.
5. Links and literature
1. Overview

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

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

It is recommended to use Calendar if possible. Existing API may required and Date Calendar 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 Formatdateformat df1 = new SimpleDateFormat ("Dd/mm/yy"); String formattedDate1 = Df1.format (New Date ()),/or use YYYY/MM/DD as Formatdateformat df2 = new SimpleDateFormat ("yyyy/m M/dd "); String FormattedDate2 = Df2.format (thedate);  

  

3. Working with Dates and Calendars3.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

  

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 (n.);    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 ("Yyyym Mdd ");    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) {Simpledatefo    Rmat 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 ();  }}

  

Java Date, Calendar and Time api-tutorial

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.