3.Java Base Date Object

Source: Internet
Author: User
Tags dateformat days in month time and date

Bi Xiangdong Teacher Java Basic Learning Note--date Object

Today, after learning the date object in Java, I feel that this object is mainly useful to us in the following points.

1. Get the time and date and display it in your own defined format.

2. The time is displayed when the website is designed.

The knowledge points are as follows:

Java.util

the Date class represents a specific moment, accurate to milliseconds.

Prior to JDK 1.1, the class Date had two other functions. It allows you to interpret dates as year, month, day, hour, minute, and second values. It also allows formatting and parsing of date strings. However, the APIs for these functions are not easy to internationalize. Starting with JDK 1.1, you should use Calendar classes to convert between date and Time fields, using DateFormat classes to format and parse date strings. Dateis deprecated in the appropriate method.

Java.text

Class DateFormat

DateFormat is an abstract class of date/time formatting subclasses that formats and resolves dates or times in a language-independent manner. Date/time formatting subclasses (such as SimpleDateFormat) allow formatting (that is, date-and text), parsing (text-to-date), and normalization. Represents a date as an Date object, or as the number of milliseconds from GMT (Greenwich Mean Time) January 1, 1970 00:00:00 This moment.


public class SimpleDateFormat extends DateFormat

     SimpleDateFormatis a specific class that formats and resolves dates in a language-related way. It allows formatting (date-and text), parsing (text-to-date), and normalization. SimpleDateFormatallows you to select any user-defined pattern of date-time formats. However, it is still recommended DateFormat getTimeInstance getDateInstance getDateTimeInstance to create a date-time formatter by using the, or. Each of these class methods can return a date/time formatter initialized in the default format pattern. You can use methods as needed applyPattern to modify the format pattern.

Date and Time mode

Date and time formats are specified by a date and time pattern string. In a date and time pattern string, the unquoted letters ‘A‘ to ‘Z‘ and ‘a‘ to ‘z‘ are interpreted as pattern letters, which are used to represent date or time string elements. Text can be caused by using single quotation marks ( ) to avoid interpretation. "‘‘"represents a single quotation mark. All other characters are not interpreted; Simply copy them to the output string when they are formatted, or match the input string when parsing.

The following pattern letters are defined (all other characters ‘A‘ to ‘Z‘ and ‘a‘ to ‘z‘ are preserved):

Letters date or time element represents Example
G Era Marker Text AD
y Years Year 1996;96
M Month of the Year Month July; Jul;07
w Number of weeks in the year Number 27
W Number of weeks in the month Number 2
D Number of days in the year Number 189
d Number of days in month Number 10
F Week of the Month Number 2
E Days of the Week Text Tuesday;Tue
a AM/PM Mark Text PM
H Number of hours in the day (0-23) Number 0
k Number of hours in the day (1-24) Number 24
K Number of hours in am/pm (0-11) Number 0
h Number of hours in am/pm (1-12) Number 12
m Number of minutes in hours Number 30
s Number of seconds in minutes Number 55
S Number of milliseconds Number 978
z Time General Time Zone Pacific Standard Time; PST;GMT-08:00
Z Time RFC 822 Time Zone -0800

Sample source code is as follows:

/**The ****************************** Date object is DateFormat in the Java.text package in the Java.util package ********************************/Importjava.text.*;ImportJava.util.*;classdatedemo{ Public Static voidMain (string[] args) {Date d=NewDate (); System.out.println (d);//printing time can not understand, want some format. //encapsulates a pattern into a SimpleDateFormat object. SimpleDateFormat sdf=NewSimpleDateFormat ("yyyy mm month DD Day"); //calls the format method to reformat the specified Date object. String Time=Sdf.format (d); System.out.println ("Time=" +Time ); }}

Program run:

3.Java Base Date Object

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.