Date and time of treatment

sponsored links
Transfer from: http://zangweiren.javaeye.com/blog/218264

Date and time of treatment not only in the face to the questions in the examination, in the actual project development is what we often need to address the problem, it seems that no project can avoid them, we often deal with the user's date of birth, registration date, order The attributes used to create time, we can see its importance.

java.util.Date class

Mentioned date and time, I think we should first think of it java.util.Date class. Date class can be accurate to milliseconds, the number of milliseconds that is relative to Greenwich Mean Time "1970-01-01 00:00:00.000 GMT" difference. So, what is GMT time? To answer this question, we need first to look at the world time standard of knowledge.

There are the world time standard UTC, or Coordinated Universal Time (Chinese name is translated as Universal Coordinated Time, the world's unity of time or Universal Time) and GMT, or Greenwich Mean Time (Chinese name is translated as Greenwich Mean Time or Greenwich mean time ) two. Strictly speaking, UTC more accurate than the GMT of some, but their difference is not more than 0.9 seconds, if exceeded, will increase the leap second to UTC and GMT, the Earth's rotation period is consistent. Therefore, in daily use, we can look the same as UTC and GMT.

Date and time that is when we are in zone associated, if we do not specify the time zone, then they will be the system default time zone to display. Let's take a look at how to create a Date object. Date class constructor method, many months, most have not been in favor of using the (Deprecated), but also the remaining two can be used:
Java code
public Date () (
this (System.currentTimeMillis ());
)

public Date (long date) (
/ / Other code
)

public Date () (
this (System.currentTimeMillis ());
)

public Date (long date) (
/ / Other code
)

The first is the constructor without parameters, using the system current time in milliseconds to create a Date object, it calls java.lang.System class currentTimeMillis () to get the system's current time in milliseconds value. This is a local method, which is defined as follows:
Java code
public static native long currentTimeMillis ();

public static native long currentTimeMillis ();

The second constructor is based on a given number of milliseconds to create a corresponding Date object, the number of milliseconds that determines the object is created the year, month, day, hour, minute and second value of the property.

We take a look at the date and time in default under the display time zone:
Java code
import java.util.Date;

public class DateTest (
public static void main (String [] args) (
Date d = new Date ();
/ / The default time zone value of the output date and time
System.out.println (d);
)
)

import java.util.Date;

public class DateTest (
public static void main (String [] args) (
Date d = new Date ();
/ / The default time zone value of the output date and time
System.out.println (d);
)
)

Run Results:

Tue Jul 22 10:44:47 CST 2008

We should note that the years before the "CST" logo, which is the acronym for China Standard Time refers to the Chinese Standard Time, is what we often say Beijing. Time difference with UTC is UTC +8:00, say Beijing earlier than the world standard 8 hours, if the world standard time is 1:00 am, Beijing time is 9 am. Under normal circumstances we do not care about time zones.

End Date object is created, we can call the getTime () method to get the millisecond value of the object, call the setTime (long time) methods to set its millisecond value, thus affecting the year, month, day, hour, minute and second of these attributes. These two methods defined as follows:
Java code
public long getTime () (
/ / Other code
)

public void setTime (long time) (
/ / Other code
)

public long getTime () (
/ / Other code
)

public void setTime (long time) (
/ / Other code
)

Sheng Date object can be expressed as relative to "1970-01-01 00:00:00.000 GMT" number of milliseconds, we can, to compare this value by the size of the two dates, but the date is concerned, to say before and after be more appropriate. The Date class has provided us with this method:
Java code
public boolean before (Date when) (
/ / Other code
)

public boolean after (Date when) (
/ / Other code
)

public int compareTo (Date anotherDate) (
/ / Other code
)

public boolean before (Date when) (
/ / Other code
)

public boolean after (Date when) (
/ / Other code
)

public int compareTo (Date anotherDate) (
/ / Other code
)

before () is the parameter to judge whether the current date before the date that is less than the parameters of the current number of milliseconds that the date of the date of milliseconds; after () is the parameter to judge whether the current date after the date that the number of milliseconds greater than the parameters of the current date Date milliseconds. The compareTo () is to compare the current date and the date parameters, return an int type value, its return value, there are three possibilities: -1,0 and 1. Return -1 if the current date, said date in the parameters; return 0 if the two dates is the same time that; return a date in the parameters that the current date. While we can use compareTo () method to compare two Date objects, but its design is actually another purpose, we will be mentioned in later chapters.

Here we use an example to test about the use of these methods:
Java code
import java.util.Date;

public class DateTest (
public static void main (String [] args) (
/ / 2008-08-08 20:00:00 correspond to the number of milliseconds
long t2008 = 1218196800000L;
/ / 1900-01-01 20:00:00 correspond to the number of milliseconds
long t1900 =-2208945952000L;

/ / Create a Date object specified number of milliseconds
Date d2008 = new Date (t2008);
/ / Use the system default time to create a Date object
Date d1900 = new Date ();
/ / By setting the number of milliseconds to change the date and time
d1900.setTime (t1900);

System.out.println ("call the method: d1900.before (d2008)");
System.out
. Print ("comparison: \" 1900-01-01 20:00:00 \ "in \" 2008-08-08 20:00:00 \ "");
/ / Use before () Methods
if (d1900.before (d2008)) (
System.out.println ("before");
) Else (
System.out.println ("after");
)

System.out.println ();

System.out.println ("call the method: d2008.after (d1900)");
System.out
. Print ("comparison: \" 2008-08-08 20:00:00 \ "in \" 1900-01-01 20:00:00 \ "");
/ / Use after () Methods
if (d2008.after (d1900)) (
System.out.println ("after");
) Else (
System.out.println ("before");
)

System.out.println ();

System.out.println ("call the method: d1900.compareTo (d2008)");
System.out
. Print ("comparison: \" 1900-01-01 20:00:00 \ "in \" 2008-08-08 20:00:00 \ "");
/ / Use compareTo () Methods
int i = d1900.compareTo (d2008);
if (i == -1) (
System.out.println ("before");
) Else if (i == 1) (
System.out.println ("after");
) Else if (i == 0) (
System.out.println ("same time");
)
)
)

import java.util.Date;

public class DateTest (
public static void main (String [] args) (
/ / 2008-08-08 20:00:00 correspond to the number of milliseconds
long t2008 = 1218196800000L;
/ / 1900-01-01 20:00:00 correspond to the number of milliseconds
long t1900 =-2208945952000L;

/ / Create a Date object specified number of milliseconds
Date d2008 = new Date (t2008);
/ / Use the system default time to create the Date object
Date d1900 = new Date ();
/ / By setting the number of milliseconds to change the date and time
d1900.setTime (t1900);

System.out.println ("call the method: d1900.before (d2008)");
System.out
. Print ("comparison: \" 1900-01-01 20:00:00 \ "in \" 2008-08-08 20:00:00 \ "");
/ / Use before () Methods
if (d1900.before (d2008)) (
System.out.println ("before");
) Else (
System.out.println ("after");
)

System.out.println ();

System.out.println ("call the method: d2008.after (d1900)");
System.out
. Print ("comparison: \" 2008-08-08 20:00:00 \ "in \" 1900-01-01 20:00:00 \ "");
/ / Use after () Methods
if (d2008.after (d1900)) (
System.out.println ("after");
) Else (
System.out.println ("before");
)

System.out.println ();

System.out.println ("call the method: d1900.compareTo (d2008)");
System.out
. Print ("comparison: \" 1900-01-01 20:00:00 \ "in \" 2008-08-08 20:00:00 \ "");
/ / Use compareTo () Methods
int i = d1900.compareTo (d2008);
if (i == -1) (
System.out.println ("before");
) Else if (i == 1) (
System.out.println ("after");
) Else if (i == 0) (
System.out.println ("same time");
)
)
)

Run Results:

Call the method: d1900.before (d2008)
Results of the comparison: "1900-01-01 20:00:00" in the "2008-08-08 20:00:00" before

Call the method: d2008.after (d1900)
Results of the comparison: "2008-08-08 20:00:00" in the "1900-01-01 20:00:00" after

Call the method: d1900.compareTo (d2008)
Results of the comparison: "1900-01-01 20:00:00" in the "2008-08-08 20:00:00" before

So if we want to directly access or change the year, month, day, hour, minute, second, etc. The value of these attributes how do? Date of course completion of these operations class methods, but unfortunately they have not been in favor of the use. We must provide these operations for a class, this class is java.util.Calendar.

Gregorian calendar java.util.GregorianCalendar

Calendar is an abstract class, we can not instantiate it directly, it has a specific sub-class of entity class java.util.GregorianCalendar, this class is the implementation of the Gregorian calendar used in our daily calendar, or called the Gregorian calendar. We can create it using the new command instance, or use the Calendar class example of this method to obtain it:
Java code
public static Calendar getInstance () (
/ / Other code
)

public static Calendar getInstance () (
/ / Other code
)

This method used above, we created Calendar object date and time value is the object is created, the system date and time value. When using the new commands, we have two options, one is currently using the system date and time value of GregorianCalendar object initialization; the other is through a given year, month, day, hour, minute, seconds and other property values initialized. Consider the following example:
Java code
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class DateTest (
/ **
* A more friendly way to format the date and time values
*
* @ Param c
* Date and time object
* @ Return string formatted date and time
* /
public static String toFriendlyString (Calendar c) (
if (c! = null) (
DateFormat df = new SimpleDateFormat ("yyyy month dd day in MM HH: mm: ss");
return df.format (c.getTime ());
)
return null;
)

public static void main (String [] args) (
Calendar c1 = Calendar.getInstance ();
System.out.println ("Creating form: Calendar.getInstance ()");
System.out.println ("Date Time:" + DateTest.toFriendlyString (c1));
System.out.println ();

Calendar c2 = new GregorianCalendar ();
System.out.println ("Creating form: new GregorianCalendar ()");
System.out.println ("Date Time:" + DateTest.toFriendlyString (c2));
System.out.println ();

/ / Parameter means the following order: year, month, day
Calendar c3 = new GregorianCalendar (2008, 8,;
System.out.println ("Creating form: new GregorianCalendar (2008, 8,");
System.out.println ("Date Time:" + DateTest.toFriendlyString (c3));
System.out.println ();

/ / Parameter means the following order: year, month, day, hour, minute
Calendar c4 = new GregorianCalendar (2008, 8, 8, 6, 10);
System.out.println ("Creating form: new GregorianCalendar (2008, 8, 8, 6, 10)");
System.out.println ("Date Time:" + DateTest.toFriendlyString (c4));
System.out.println ();

/ / Parameter means the following order: year, month, day, hour, minute, second
Calendar c5 = new GregorianCalendar (2008, 8, 8, 18, 10, 5);
System.out.println ("Creating form: new GregorianCalendar (2008, 8, 8, 18, 10, 5)");
System.out.println ("Date Time:" + DateTest.toFriendlyString (c5));
)
)

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class DateTest (
/ **
* A more friendly way to format the date and time values
*
* @ Param c
* Date and time object
* @ Return string formatted date and time
* /
public static String toFriendlyString (Calendar c) (
if (c! = null) (
DateFormat df = new SimpleDateFormat ("yyyy month dd day in MM HH: mm: ss");
return df.format (c.getTime ());
)
return null;
)

public static void main (String [] args) (
Calendar c1 = Calendar.getInstance ();
System.out.println ("Creating form: Calendar.getInstance ()");
System.out.println ("Date Time:" + DateTest.toFriendlyString (c1));
System.out.println ();

Calendar c2 = new GregorianCalendar ();
System.out.println ("Creating form: new GregorianCalendar ()");
System.out.println ("Date Time:" + DateTest.toFriendlyString (c2));
System.out.println ();

/ / Parameter means the following order: year, month, day
Calendar c3 = new GregorianCalendar (2008, 8,;
System.out.println ("Creating form: new GregorianCalendar (2008, 8,");
System.out.println ("Date Time:" + DateTest.toFriendlyString (c3));
System.out.println ();

/ / Parameter means the following order: year, month, day, hour, minute
Calendar c4 = new GregorianCalendar (2008, 8, 8, 6, 10);
System.out.println ("Creating form: new GregorianCalendar (2008, 8, 8, 6, 10)");
System.out.println ("Date Time:" + DateTest.toFriendlyString (c4));
System.out.println ();

/ / Parameter means the following order: year, month, day, hour, minute, second
Calendar c5 = new GregorianCalendar (2008, 8, 8, 18, 10, 5);
System.out.println ("Creating form: new GregorianCalendar (2008, 8, 8, 18, 10, 5)");
System.out.println ("Date Time:" + DateTest.toFriendlyString (c5));
)
)

The results are:

Create ways: Calendar.getInstance ()
Date Time: July 22, 2008 11:54:48

Create ways: new GregorianCalendar ()
Date Time: July 22, 2008 11:54:48

Create ways: new GregorianCalendar (2008, 8,
Date Time: September 8, 2008 00:00:00

Create ways: new GregorianCalendar (2008, 8, 8, 6, 10)
Date Time: September 8, 2008 06:10:00

Create ways: new GregorianCalendar (2008, 8, 8, 18, 10, 5)
Date Time: September 8, 2008 18:10:05

For ease of reading, we add a toFriendlyString (Calendar c) method, it will date and time value formatted as a more friendly and understandable form, we will explain the contents of the coming implementation of its principles. Analysis of operating results, we found that there are two areas that need attention:

GregorianCalendar object is created, the month value is set to 8, but the results are printed in September. This is not a problem with our code, but because JAVA, said the month is starting from 0, and that it always used to represent the month value smaller than the actual value of 1 month. So we said in August that should set the value 8-1 = 7.
GregorianCalendar number of hours is 24 hour clock.

In order to avoid occur because forget the difference between treatment 1 was established for the wrong of the month, let code looks more intuitive, recommend you Shiyongdingyi Zai Calendar Lei of the Zhexie constant Lai instead of direct numerical month:

January: Calendar.JANUARY = 0
February: Calendar.FEBRUARY = 1
March: Calendar.MARCH = 2
April: Calendar.APRIL = 3
May: Calendar.MAY = 4
June: Calendar.JUNE = 5
July: Calendar.JULY = 6
August: Calendar.AUGUST = 7
September: Calendar.SEPTEMBER = 8
October: Calendar.OCTOBER = 9
November: Calendar.NOVEMBER = 10
December: Calendar.DECEMBER = 11

If we want to from the Calendar object can acquire the various properties of the De value, Jiu need to call the Ta of the get (int field) Fangfa, Zhege method Jieshou an int Type of Shen Shu, Bingjugenju this parameter's value Lai Ji Ding return the property value corresponding . This method is defined as follows:
Java code
public int get (int field) (
/ / Other code
)

public int get (int field) (
/ / Other code
)

We are an example to illustrate the get (int field) method can accept some common meaning and usage parameters:
Java code
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateTest (
/ **
* A more friendly way to format the date and time values
*
* @ Param c
* Date and time object
* @ Return string formatted date and time
* /
public static String toFriendlyString (Calendar c) (
if (c! = null) (
DateFormat df = new SimpleDateFormat ("yyyy month dd day in MM HH: mm: ss.SSS");
return df.format (c.getTime ());
)
return null;
)

public static void main (String [] args) (
Calendar c = Calendar.getInstance ();
System.out.println ("Current time:" + DateTest.toFriendlyString (c));
System.out.println ();

System.out.println ("Attribute name: Calendar.AM_PM");
System.out.println ("on behalf of meaning: on the afternoon of identification in the morning to return Calendar.AM = 0, afternoon return to Calendar.PM = 1");
System.out.println ("test:" + c.get (Calendar.AM_PM));
System.out.println ();

System.out.println ("Attribute name: Calendar.DATE");
System.out.println ("on behalf of meanings: the first days of the month, with Calendar.DAY_OF_MONTH");
System.out.println ("test:" + c.get (Calendar.DATE));
System.out.println ();

System.out.println ("Attribute name: Calendar.DAY_OF_MONTH");
System.out.println ("on behalf of meanings: the first days of the month, with Calendar.DATE");
System.out.println ("test:" + c.get (Calendar.DAY_OF_MONTH));
System.out.println ();

System.out.println ("Attribute name: Calendar.DAY_OF_WEEK");
System.out.println ("on behalf of meanings: the first days of the week, the corresponding day of the week, Sunday the first day, this analogy.");
System.out.println ("Sunday: Calendar.SUNDAY = 1");
System.out.println ("Monday: Calendar.MONDAY = 2");
System.out.println ("Tuesday: Calendar.TUESDAY = 3");
System.out.println ("Wednesday: Calendar.WEDNESDAY = 4");
System.out.println ("Thursday: Calendar.THURSDAY = 5");
System.out.println ("Friday: Calendar.FRIDAY = 6");
System.out.println ("Saturday: Calendar.SATURDAY = 7");
System.out.println ("test:" + c.get (Calendar.DAY_OF_WEEK));
System.out.println ();

System.out.println ("Attribute name: Calendar.DAY_OF_WEEK_IN_MONTH");
System.out.println ("on behalf of meanings: the day of the week corresponding to the first few months there");
System.out.println ("test:" + c.get (Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println ();

System.out.println ("Attribute name: Calendar.DAY_OF_YEAR");
System.out.println ("on behalf of meanings: the first days of the year");
System.out.println ("test:" + c.get (Calendar.DAY_OF_YEAR));
System.out.println ();

System.out.println ("Attribute name: Calendar.HOUR");
System.out.println ("on behalf of meanings: a 12-hour number of hours under the noon and midnight is represented as 0");
System.out.println ("test:" + c.get (Calendar.HOUR));
System.out.println ();

System.out.println ("Attribute name: Calendar.HOUR_OF_DAY");
System.out.println ("on behalf of meanings: a 24-hour number of hours under the midnight express to 0");
System.out.println ("test:" + c.get (Calendar.HOUR_OF_DAY));
System.out.println ();

System.out.println ("Attribute name: Calendar.MILLISECOND");
System.out.println ("on behalf of meaning: the number of milliseconds");
System.out.println ("test:" + c.get (Calendar.MILLISECOND));
System.out.println ();

System.out.println ("Attribute name: Calendar.MINUTE");
System.out.println ("on behalf of meanings: minutes");
System.out.println ("test:" + c.get (Calendar.MINUTE));
System.out.println ();

System.out.println ("Attribute name: Calendar.MONTH");
System.out.println ("on behalf of meaning: the month, from 0-11 said 12 months, the month than the actual value of a small 1");
System.out.println ("test:" + c.get (Calendar.MONTH));
System.out.println ();

System.out.println ("Attribute name: Calendar.SECOND");
System.out.println ("on behalf of meanings: seconds");
System.out.println ("test:" + c.get (Calendar.SECOND));
System.out.println ();

System.out.println ("Attribute name: Calendar.WEEK_OF_MONTH");
System.out.println ("on behalf of meanings: the first few weeks of the month");
System.out.println ("test:" + c.get (Calendar.WEEK_OF_MONTH));
System.out.println ();

System.out.println ("Attribute name: Calendar.WEEK_OF_YEAR");
System.out.println ("on behalf of meanings: the first few weeks of the year");
System.out.println ("test:" + c.get (Calendar.WEEK_OF_YEAR));
System.out.println ();

System.out.println ("Attribute name: Calendar.YEAR");
System.out.println ("on behalf of meanings: the year");
System.out.println ("test:" + c.get (Calendar.YEAR));
)
)

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateTest (
/ **
* A more friendly way to format the date and time values
*
* @ Param c
* Date and time object
* @ Return string formatted date and time
* /
public static String toFriendlyString (Calendar c) (
if (c! = null) (
DateFormat df = new SimpleDateFormat ("yyyy month dd day in MM HH: mm: ss.SSS");
return df.format (c.getTime ());
)
return null;
)

public static void main (String [] args) (
Calendar c = Calendar.getInstance ();
System.out.println ("Current time:" + DateTest.toFriendlyString (c));
System.out.println ();

System.out.println ("Attribute name: Calendar.AM_PM");
System.out.println ("on behalf of meaning: on the afternoon of identification in the morning to return Calendar.AM = 0, afternoon return to Calendar.PM = 1");
System.out.println ("test:" + c.get (Calendar.AM_PM));
System.out.println ();

System.out.println ("Attribute name: Calendar.DATE");
System.out.println ("on behalf of meanings: the first days of the month, with Calendar.DAY_OF_MONTH");
System.out.println ("test:" + c.get (Calendar.DATE));
System.out.println ();

System.out.println ("Attribute name: Calendar.DAY_OF_MONTH");
System.out.println ("on behalf of meanings: the first days of the month, with Calendar.DATE");
System.out.println ("test:" + c.get (Calendar.DAY_OF_MONTH));
System.out.println ();

System.out.println ("Attribute name: Calendar.DAY_OF_WEEK");
System.out.println ("on behalf of meanings: the first days of the week, the corresponding day of the week, Sunday the first day, this analogy.");
System.out.println ("Sunday: Calendar.SUNDAY = 1");
System.out.println ("Monday: Calendar.MONDAY = 2");
System.out.println ("Tuesday: Calendar.TUESDAY = 3");
System.out.println ("Wednesday: Calendar.WEDNESDAY = 4");
System.out.println ("Thursday: Calendar.THURSDAY = 5");
System.out.println ("Friday: Calendar.FRIDAY = 6");
System.out.println ("Saturday: Calendar.SATURDAY = 7");
System.out.println ("test:" + c.get (Calendar.DAY_OF_WEEK));
System.out.println ();

System.out.println ("Attribute name: Calendar.DAY_OF_WEEK_IN_MONTH");
System.out.println ("on behalf of meanings: the day of the week corresponding to the first few months there");
System.out.println ("test:" + c.get (Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println ();

System.out.println ("Attribute name: Calendar.DAY_OF_YEAR");
System.out.println ("on behalf of meanings: the first days of the year");
System.out.println ("test:" + c.get (Calendar.DAY_OF_YEAR));
System.out.println ();

System.out.println ("Attribute name: Calendar.HOUR");
System.out.println ("on behalf of meanings: a 12-hour number of hours under the noon and midnight is represented as 0");
System.out.println ("test:" + c.get (Calendar.HOUR));
System.out.println ();

System.out.println ("Attribute name: Calendar.HOUR_OF_DAY");
System.out.println ("on behalf of meanings: a 24-hour number of hours under the midnight express to 0");
System.out.println ("test:" + c.get (Calendar.HOUR_OF_DAY));
System.out.println ();

System.out.println ("Attribute name: Calendar.MILLISECOND");
System.out.println ("on behalf of meaning: the number of milliseconds");
System.out.println ("test:" + c.get (Calendar.MILLISECOND));
System.out.println ();

System.out.println ("Attribute name: Calendar.MINUTE");
System.out.println ("on behalf of meanings: minutes");
System.out.println ("test:" + c.get (Calendar.MINUTE));
System.out.println ();

System.out.println ("Attribute name: Calendar.MONTH");
System.out.println ("on behalf of meaning: the month, from 0-11 said 12 months, the month than the actual value of a small 1");
System.out.println ("test:" + c.get (Calendar.MONTH));
System.out.println ();

System.out.println ("Attribute name: Calendar.SECOND");
System.out.println ("on behalf of meanings: seconds");
System.out.println ("test:" + c.get (Calendar.SECOND));
System.out.println ();

System.out.println ("Attribute name: Calendar.WEEK_OF_MONTH");
System.out.println ("on behalf of meanings: the first few weeks of the month");
System.out.println ("test:" + c.get (Calendar.WEEK_OF_MONTH));
System.out.println ();

System.out.println ("Attribute name: Calendar.WEEK_OF_YEAR");
System.out.println ("on behalf of meanings: the first few weeks of the year");
System.out.println ("test:" + c.get (Calendar.WEEK_OF_YEAR));
System.out.println ();

System.out.println ("Attribute name: Calendar.YEAR");
System.out.println ("on behalf of meanings: the year");
System.out.println ("test:" + c.get (Calendar.YEAR));
)
)

The results are:

Current time: July 22, 2008 13:16:07.421

Property Name: Calendar.AM_PM
On behalf of meanings: on the afternoon of identification in the morning to return Calendar.AM = 0, afternoon return to Calendar.PM = 1
Test results: 1

Property Name: Calendar.DATE
On behalf of meanings: the first days of the month, with Calendar.DAY_OF_MONTH
Test results: 22

Property Name: Calendar.DAY_OF_MONTH
On behalf of meanings: the first days of the month, with Calendar.DATE
Test results: 22

Property Name: Calendar.DAY_OF_WEEK
On behalf of meanings: the first days of the week, the corresponding day of the week, Sunday the first day, this analogy.
Sunday: Calendar.SUNDAY = 1
Monday: Calendar.MONDAY = 2
Tuesday: Calendar.TUESDAY = 3
Wednesday: Calendar.WEDNESDAY = 4
Thursday: Calendar.THURSDAY = 5
Friday: Calendar.FRIDAY = 6
Saturday: Calendar.SATURDAY = 7
Test results: 3

Property Name: Calendar.DAY_OF_WEEK_IN_MONTH
On behalf of meanings: the day of the week corresponding to the first few months in the test results appear: 4

Property Name: Calendar.DAY_OF_YEAR
On behalf of meanings: the first days of the year results: 204

Property Name: Calendar.HOUR
Representative means: under 12-hour hours, at noon and midnight is represented as 0
Test results: 1

Property Name: Calendar.HOUR_OF_DAY
On behalf of meanings: a 24-hour number of hours under the midnight express to 0
Test results: 13

Property Name: Calendar.MILLISECOND
Representative means: number of milliseconds the test results: 421

Property Name: Calendar.MINUTE
Representative means: min test results: 16

Property Name: Calendar.MONTH
On behalf of meanings: the month, from 0 to 11 said 12 months, than the actual value of a small one month
Test results: 6

Property Name: Calendar.SECOND
Representative means: second test results: 7

Property Name: Calendar.WEEK_OF_MONTH
On behalf of meanings: one month in the first few weeks of test results: 4

Property Name: Calendar.WEEK_OF_YEAR
On behalf of meanings: the first few weeks of the year results: 30

Property Name: Calendar.YEAR
Representative means: Year Test Results: 2008

One more difficult to understand the meaning of the representative Calendar.DAY_OF_WEEK_IN_MONTH some, it means "the day of the week corresponding to the first few months there." Such as August 8, 2008 was a Friday, before it is Friday, August 1st, so it is the second Friday in August. So then call get (Calendar.DAY_OF_WEEK_IN_MONTH) will return 2. There's a simple rule to remember: For the month 1-7, they certainly accounted for a Monday to Sunday, so either they are in the day, no matter which day of the week, it was always the first , so return 1; 8-14 also accounted for a Monday to Sunday, but the relationship between 1-7, for they always return 2; and so on, 15-21 returned to No. 3,22-28 Back 4,29-31 returns No. 5.

Date Calendar objects and class objects are by Calendar Conversion between two methods:
Java code
public final Date getTime () (
/ / Other code
)

public final void setTime (Date date) (
/ / Other code
)

public final Date getTime () (
/ / Other code
)

public final void setTime (Date date) (
/ / Other code
)

Date formatting and parsing

We go back again to see the example above, the definition of toFriendlyString (Calendar c) method, It includes a Calendar Duixiang the Riqi time values of one kind of very friendly way to show, it's easy to understand as well as ours Chinese habits. This is entirely due to the abstract class DateFormat and its sub-class entity class SimpleDateFormat help. Both classes are located in the java.text package, is dedicated to the date formatting and parsing of the class. The core of these two tasks we set for the Pattern, we can call "date format expression."

In theory, the date format pattern contains all 26 letters of the case, but some of them were reserved for letters only, and no exact meaning. Currently effective letters and they represent the following meanings:

G:'s logo, that was BC or AD
y: year
M: month
d: Day
h: hours, from 1 to 12, morning and afternoon
H: hour, from 0 to 23
m: minutes
s: seconds
S: ms
E: The first days of the week, the corresponding day of the week, Sunday the first day, this analogy
z: Time Zone
D: The first days of the year
F: The day of the week corresponding to the first few months there
w: the first few weeks of the year
W: The first few weeks of the month
a: AM / PM Logo
k: hours from 1 to 24
K: hour, from 0 to 11, distinguish between morning and afternoon

The date format of expression for all the letters appear, after carrying out the date formatting, will be their representative Hanyiduiying of Zhuxingzhisuo replacement, and for some Zimulaiyue number of repeat differences after Ge Shihua The results will be different. Consider the following example:
Java code
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTest (
public static void main (String [] args) (
/ / The current date and time values using the system to create a Date object
Date now = new Date ();

/ / Create a date format of expression
String pattern = "BC: G; Year: y; month: M; day: d; time (1 ~ 12): h; pm (0 ~ 23): H; points: m; s: s; ms: S ; week: E; / pm: a; time zone: z ";
/ / Date format used to create a SimpleDateFormat object expression
SimpleDateFormat df = new SimpleDateFormat (pattern);
/ / Call the SimpleDateFormat class format (Date date) method of the Date object to format, and returns the formatted string.
/ / This method is inherited from java.text.DateFormat class
System.out.println ("1 Bit :" + df.format (now));

/ / Create a new date format expression
pattern = "BC: GG; Year: yy; month: MM; Date: dd; time (1 ~ 12): hh; pm (0 ~ 23): HH; points: mm; seconds: ss; ms: SS; week: EE; / pm: aa; time zone: zz ";
/ / Call the SimpleDateFormat's applyPattern (String pattern) method to create the date format with a new expression to replace the original
df.applyPattern (pattern);
System.out.println ("2 Bit :" + df.format (now));

pattern = "BC: GGG; Year: yyy; month: MMM; Date: ddd; time (1 ~ 12): hhh; pm (0 ~ 23): HHH; points: mmm; seconds: sss; ms: SSS; week: EEE; / pm: aaa; time zone: zzz ";
df.applyPattern (pattern);
System.out.println ("3 Bit :" + df.format (now));

pattern = "BC: GGGG; Year: yyyy; month: MMMM; Date: dddd; time (1 ~ 12): hhhh; pm (0 ~ 23): HHHH; points: mmmm; seconds: ssss; ms: SSSS; week: EEEE; / pm: aaaa; time zone: zzzz ";
df.applyPattern (pattern);
System.out.println ("4 Bit :" + df.format (now));

pattern = "BC: GGGGG; Year: yyyyy; month: MMMMM; Date: ddddd; time (1 ~ 12): hhhhh; pm (0 ~ 23): HHHHH; points: mmmmm; seconds: sssss; ms: SSSSS; week: EEEEE; / pm: aaaaa; time zone: zzzzz ";
df.applyPattern (pattern);
System.out.println ("5 Bit :" + df.format (now));

pattern = "BC: GGGGGG; Year: yyyyyy; month: MMMMMM; Date: dddddd; time (1 ~ 12): hhhhhh; pm (0 ~ 23): HHHHHH; points: mmmmmm; seconds: ssssss; ms: SSSSSS; week: EEEEEE; / pm: aaaaaa; time zone: zzzzzz ";
df.applyPattern (pattern);
System.out.println ("6 Bit :" + df.format (now));
)
)

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTest (
public static void main (String [] args) (
/ / The current date and time values using the system to create a Date object
Date now = new Date ();

/ / Create a date format of expression
String pattern = "BC: G; Year: y; month: M; day: d; time (1 ~ 12): h; pm (0 ~ 23): H; points: m; s: s; ms: S ; week: E; / pm: a; time zone: z ";
/ / Date format used to create a SimpleDateFormat object expression
SimpleDateFormat df = new SimpleDateFormat (pattern);
/ / Call the SimpleDateFormat class format (Date date) method of the Date object to format, and returns the formatted string.
/ / This method is inherited from java.text.DateFormat class
System.out.println ("1 Bit :" + df.format (now));

/ / Create a new date format expression
pattern = "BC: GG; Year: yy; month: MM; Date: dd; time (1 ~ 12): hh; pm (0 ~ 23): HH; points: mm; seconds: ss; ms: SS; week: EE; / pm: aa; time zone: zz ";
/ / Call the SimpleDateFormat's applyPattern (String pattern) method to create the date format with a new expression to replace the original
df.applyPattern (pattern);
System.out.println ("2 Bit :" + df.format (now));

pattern = "BC: GGG; Year: yyy; month: MMM; Date: ddd; time (1 ~ 12): hhh; pm (0 ~ 23): HHH; points: mmm; seconds: sss; ms: SSS; week: EEE; / pm: aaa; time zone: zzz ";
df.applyPattern (pattern);
System.out.println ("3 Bit :" + df.format (now));

pattern = "BC: GGGG; Year: yyyy; month: MMMM; Date: dddd; time (1 ~ 12): hhhh; pm (0 ~ 23): HHHH; points: mmmm; seconds: ssss; ms: SSSS; week: EEEE; / pm: aaaa; time zone: zzzz ";
df.applyPattern (pattern);
System.out.println ("4 Bit :" + df.format (now));

pattern = "BC: GGGGG; Year: yyyyy; month: MMMMM; Date: ddddd; time (1 ~ 12): hhhhh; pm (0 ~ 23): HHHHH; points: mmmmm; seconds: sssss; ms: SSSSS; week: EEEEE; / pm: aaaaa; time zone: zzzzz ";
df.applyPattern (pattern);
System.out.println ("5 Bit :" + df.format (now));

pattern = "BC: GGGGGG; Year: yyyyyy; month: MMMMMM; Date: dddddd; time (1 ~ 12): hhhhhh; pm (0 ~ 23): HHHHHH; points: mmmmmm; seconds: ssssss; ms: SSSSSS; week: EEEEEE; / pm: aaaaaa; time zone: zzzzzz ";
df.applyPattern (pattern);
System.out.println ("6 Bit :" + df.format (now));
)
)

The output is as follows:

1: BC: AD; Year: 08; Month: 7; Date: 22; pm (1 ~ 12): 3; pm (0 ~ 23): 15; points: 17; seconds: 49; ms: 187; week: Tuesday; on / at: pm; Time Zone: CST
2: BC: AD; Year: 08; month: 07; Date: 22; pm (1 ~ 12): 03; pm (0 ~ 23): 15; points: 17; seconds: 49; ms: 187; week: Tuesday; on / at: pm; Time Zone: CST
3: BC: AD; Year: 08; Month: July; Day: 022; pm (1 ~ 12): 003; pm (0 ~ 23): 015; points: 017; seconds: 049; ms: 187 ; week: Tuesday; on / at: pm; Time Zone: CST
4: BC: AD; Year: 2008; Month: July; date: 0022; pm (1 ~ 12): 0003; pm (0 ~ 23): 0015; points: 0017; seconds: 0049; ms: 0187 ; week: Tuesday; on / at: pm; Time Zone: China Standard Time
5: BC: AD; Year: 02,008; Month: July; Date: 00022; pm (1 ~ 12): 00003; pm (0 ~ 23): 00015; points: 00017; seconds: 00049; ms: 00187 ; week: Tuesday; on / at: pm; Time Zone: China Standard Time
6: BC: AD; Year: 002,008; Month: July; Day: 000 022; pm (1 ~ 12): 000 003; pm (0 ~ 23): 000 015; points: 000 017; seconds: 000049; ms: 000 187 ; week: Tuesday; on / at: pm; Time Zone: China Standard Time

If we want to output the original letter, not the meaning they represent the replacement value, you need to use single quotes to them included, same is true for reserved characters, although they do not have the exact meaning. A pair of single quotes can contain more than one letter, and two consecutive single quotation marks will output the result of a single quote, double quotes will need to escape the output. For characters beyond the 26 letters can be placed on a pair of single quotes, you can also directly write. Consider the following example:
Java code
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test (
public static void main (String [] args) (
Date now = new Date ();
SimpleDateFormat df = new SimpleDateFormat (
"'YEAR': yyyy 'MONTH:'''MM'''DAY:' \" dd \ "");
System.out.println (df.format (now));
)
)

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test (
public static void main (String [] args) (
Date now = new Date ();
SimpleDateFormat df = new SimpleDateFormat (
"'YEAR': yyyy 'MONTH:'''MM'''DAY:' \" dd \ "");
System.out.println (df.format (now));
)
)

Run Results:

YEAR: 2008 MONTH: '07 'DAY: "22"

Some of the above examples, we will format the date object into a certain string output to be more consistent with our customary friendly forms. We can also, in turn, use the DateFormat class's parse (String source) method will have a certain format string into a Date object, provided that we use the expression syntax described above, the date format for finding a suitable Pattern. For example:
Java code
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTest (
public static void main (String [] args) throws ParseException (
String s = "2008-08-08";
System.out.println ("Original string:" + s);
String pattern = "yyyy-MM-dd";
System.out.println ("corresponds to the expression:" + pattern);
SimpleDateFormat df = new SimpleDateFormat (pattern);
Date date = df.parse (s);
System.out.println ("converted value:" + date);
System.out.println ();

s = "05 2/ 12 Day 18:04:33";
System.out.println ("Original string:" + s);
pattern = "yy M on d day in HH: mm: ss";
System.out.println ("corresponds to the expression:" + pattern);
df.applyPattern (pattern);
date = df.parse (s);
System.out.println ("converted value:" + date);
System.out.println ();

s = "16/5/2004 20:7:2.050";
System.out.println ("Original string:" + s);
pattern = "d / M / yyyy HH: m: s.SSS";
System.out.println ("corresponds to the expression:" + pattern);
df.applyPattern (pattern);
date = df.parse (s);
System.out.println ("converted value:" + date);
)
)

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTest (
public static void main (String [] args) throws ParseException (
String s = "2008-08-08";
System.out.println ("Original string:" + s);
String pattern = "yyyy-MM-dd";
System.out.println ("corresponds to the expression:" + pattern);
SimpleDateFormat df = new SimpleDateFormat (pattern);
Date date = df.parse (s);
System.out.println ("converted value:" + date);
System.out.println ();

s = "05 2/ 12 Day 18:04:33";
System.out.println ("Original string:" + s);
pattern = "yy M on d day in HH: mm: ss";
System.out.println ("corresponds to the expression:" + pattern);
df.applyPattern (pattern);
date = df.parse (s);
System.out.println ("converted value:" + date);
System.out.println ();

s = "16/5/2004 20:7:2.050";
System.out.println ("Original string:" + s);
pattern = "d / M / yyyy HH: m: s.SSS";
System.out.println ("corresponding expressions:" + pattern);
df.applyPattern (pattern);
date = df.parse (s);
System.out.println ("converted value:" + date);
)
)

Run Results:

The original string :2008-08-08
Corresponds to the expression: yyyy-MM-dd
Converted value: Fri Aug 08 00:00:00 CST 2008

Original string: February 12, 2005 18:04:33
Corresponds to the expression: yy M on d day in HH: mm: ss
Converted value: Sat Feb 12 18:04:33 CST 2005

Original string: 16/5/2004 20:7:2.050
Corresponds to the expression: d / M / yyyy HH: m: s.SSS
Converted value: Sun May 16 20:07:02 CST 2004
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Date and time of treatment

  • log4j easy application in java

    JAVA development, frequently used the log output, in a so-called most of the software company will have its own set of configuration style, re-read the configuration file to initialize property of the log, it will be good, but sometimes may not need to fu

  • EJB

    public transient int counter; / / transient property private String firstname; / / persistent property @ Transient String getLengthInMeter () (...) / / transient property String getName () (...) / / persistent property @ Basic int getLength () (...) / / p

  • java read file

    java read documents in two ways: java.io and java.lang.ClassLoader When using the java.io, when java.lang.ClassLoader use it? (Note: If prior to read xml file java read file clearly aware of these two methods have been like! Can take much less to understa

  • FLEX: integrating Spring + Hibernate

    Before a friend also wanted to study development of FLEX. Asked me to help him to be a small sample. Spent a weekend time, to integrate a sampleproject. Client: FLEX Server: Spring2.5 + Hibernate3.2 + Hibernate-annotations3.3.1 + MySQL5 FDS: BlazeDS3 IDE:

  • Ruby on Rails Routing - Simple Examples

    This article contains a list of ruby on rails routing examples. If you find you have any questions please leave a comment. Routes are processed from the top of routes.rb down. If a route is matched it will stop processing the routes.rb file and use that r

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • The level Hibernate cache

    Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries