ºìÁªLinuxÃÅ»§
Linux°ïÖú

Java»ù´¡£­JavaÖеÄCalendarºÍDateÀà

·¢²¼Ê±¼ä:2006-05-02 11:06:36À´Ô´:ºìÁª×÷Õß:Çç
Java ÓïÑÔµÄCalendar(ÈÕÀú),Date(ÈÕÆÚ)£¬ºÍDateFormat(ÈÕÆÚ¸ñʽ)×é³ÉÁËJava±ê×¼µÄÒ»¸ö»ù±¾µ«ÊǷdz£ÖØÒªµÄ²¿·Ö¡£ÈÕÆÚÊÇÉÌÒµÂß¼­¼ÆËãÒ»¸ö¹Ø¼üµÄ²¿·Ö¡£ËùÓеĿª·¢Õ߶¼Ó¦¸ÃÄܹ»¼ÆËãδÀ´µÄÈÕÆÚ£¬¶¨ÖÆÈÕÆÚµÄÏÔʾ¸ñʽ£¬²¢½«Îı¾Êý¾Ý½âÎö³ÉÈÕÆÚ¶ÔÏó¡£

´´½¨Ò»¸öÈÕÆÚ¶ÔÏó

ÈÃÎÒÃÇ¿´Ò»¸öʹÓÃϵͳµÄµ±Ç°ÈÕÆÚºÍʱ¼ä´´½¨Ò»¸öÈÕÆÚ¶ÔÏó²¢·µ»ØÒ»¸ö³¤ÕûÊý¡£Õâ¸öʱ¼äͨ³£±»³ÆΪJava ÐéÄâ»ú(JVM)Ö÷»ú»·¾³µÄϵͳʱ¼ä¡£

import java.util.Date;

public class DateExample1 {

public static void main(String[] args) {

// Get the system date/time

Date date = new Date();

System.out.println(date.getTime());

} }

ÔÚÐÇÆÚÁù£¬2001Äê9ÔÂ29ÈÕ£¬ÏÂÎç´óÔ¼ÊÇ6:50µÄÑù×Ó£¬ÉÏÃæµÄÀý×ÓÔÚϵͳÊä³öÉ豸ÉÏÏÔʾµÄ½á¹ûÊÇ 1001803809710¡£ÖµµÃ×¢ÒâµÄÊÇÎÒÃÇʹÓÃÁËDate ¹¹Ô캯Êý´´½¨Ò»¸öÈÕÆÚ¶ÔÏó£¬Õâ¸ö¹¹Ô캯ÊýûÓнÓÊÜÈκβÎÊý£¬¶øÕâ¸ö¹¹Ô캯ÊýÔÚÄÚ²¿Ê¹ÓÃÁËSystem.currentTimeMillis() ·½·¨À´´Óϵͳ»ñÈ¡ÈÕÆÚ¡£ÏÖÔÚÎÒÃÇÒѾ­ÖªµÀÁËÈçºÎ»ñÈ¡´Ó1970Äê1ÔÂ1ÈÕ¿ªÊ¼¾­ÀúµÄºÁÃëÊýÁË¡£ÎÒÃÇÈçºÎ²ÅÄÜÒÔÒ»ÖÖÓû§Ã÷°×µÄ¸ñʽÀ´ÏÔʾÕâ¸öÈÕÆÚÄØ? ÔÚÕâÀïÀàjava.text.SimpleDateFormat ºÍËüµÄ³éÏó»ùÀà java.text.DateFormat ¾ÍÅɵÃÉÏÓó¡ÁË¡£

ÈÕÆÚÊý¾ÝµÄ¶¨ÖƸñʽ

¼ÙÈçÎÒÃÇÏ£Íû¶¨ÖÆÈÕÆÚÊý¾ÝµÄ¸ñʽ£¬±È·½ÐÇÆÚÁù-9ÔÂ-29ÈÕ-2001Äê. ÏÂÃæµÄÀý×ÓչʾÁËÈçºÎÍê³ÉÕâ¸ö¹¤×÷:

import java.text.SimpleDateFormat;

import java.util.Date;

public class DateExample2 {

public static void main(String[] args) {

SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE-MMMM-dd-yyyy");

Date date = new Date();

System.out.println(bartDateFormat.format(date));

} }

ֻҪͨ¹ýÏòSimpleDateFormat µÄ¹¹Ô캯Êý´«µÝ¸ñʽ×Ö·û´®"EEE-MMMM-dd-yyyy"£¬ÎÒÃǾÍÄܹ»Ö¸Ã÷×Ô¼ºÏëÒªµÄ¸ñʽ¡£¸ñʽ×Ö·û´®ÖеÄASCII ×Ö·û¸æË߸ñʽ»¯º¯ÊýÏÂÃæÏÔʾÈÕÆÚÊý¾ÝµÄÄÄÒ»¸ö²¿·Ö¡£EEEEÊÇÐÇÆÚ£¬MMMMÊÇÔ£¬ddÊÇÈÕ£¬yyyyÊÇÄê¡£×Ö·ûµÄ¸öÊý¾ö¶¨ÁËÈÕÆÚÊÇÈçºÎ¸ñʽ»¯µÄ¡£´«µÝ "EE-MM-dd-yy"»áÏÔʾ Sat-09-29-01¡£

½«Îı¾Êý¾Ý½âÎö³ÉÈÕÆÚ¶ÔÏó

¼ÙÉèÎÒÃÇÓÐÒ»¸öÎı¾×Ö·û´®°üº¬ÁËÒ»¸ö¸ñʽ»¯Á˵ÄÈÕÆÚ¶ÔÏó£¬ÎÒÃÇÏ£Íû½âÎöÕâ¸ö×Ö·û´®²¢´ÓÎı¾ÈÕÆÚÊý¾Ý´´½¨Ò»¸öÈÕÆÚ¶ÔÏó¡£ÎÒÃǽ«ÔÙ´ÎÒÔ¸ñʽ»¯×Ö·û´®"MM-dd -yyyy" µ÷ÓÃSimpleDateFormatÀà¡£µ«ÊÇÕâÒ»´Î£¬ÎÒÃÇʹÓøñʽ»¯½âÎö¶ø²»ÊÇÉú³ÉÒ»¸öÎı¾ÈÕÆÚÊý¾Ý¡£ÎÒÃǵÄÀý×Ó£¬ÏÔʾÔÚÏÂÃ棬½«½âÎöÎı¾×Ö·û´®"9- 29-2001"²¢´´½¨Ò»¸öֵΪ001736000000 µÄÈÕÆÚ¶ÔÏó¡£

import java.text.SimpleDateFormat;

import java.util.Date;

public class DateExample3 {

public static void main(String[] args) {

// Create a date formatter that can parse dates of

// the form MM-dd-yyyy.

SimpleDateFormat bartDateFormat = new SimpleDateFormat("MM-dd-yyyy");

// Create a string containing a text date to be parsed.

String dateStringToParse = "9-29-2001";

try {

// Parse the text version of the date.

// We have to perform the parse method in a

// try-catch construct in case dateStringToParse

// does not contain a date in the format we are expecting.

Date date = bartDateFormat.parse(dateStringToParse);

// Now send the parsed date as a long value

// to the system output.

System.out.println(date.getTime());

}

catch (Exception ex) {

System.out.println(ex.getMessage());

}

} }

ʹÓñê×¼µÄÈÕÆÚ¸ñʽ»¯¹ý³Ì

¼ÈÈ»ÎÒÃÇÒѾ­¿ÉÒÔÉú³ÉºÍ½âÎö¶¨ÖƵÄÈÕÆÚ¸ñʽÁË£¬ÈÃÎÒÃÇÀ´¿´Ò»¿´ÈçºÎʹÓÃÄÚ½¨µÄ¸ñʽ»¯¹ý³Ì¡£·½·¨ DateFormat.getDateTimeInstance() ÈÃÎÒÃǵÃÒÔÓü¸ÖÖ²»Í¬µÄ·½·¨»ñµÃ±ê×¼µÄÈÕÆÚ¸ñʽ»¯¹ý³Ì¡£ÏÂÃæÊÇÎÒÃÇ»ñÈ¡ÁËËĸöÄÚ½¨µÄÈÕÆÚ¸ñʽ»¯¹ý³Ì¡£ËüÃÇ°üÀ¨Ò»¸ö¶ÌµÄ£¬Öеȵģ¬³¤µÄ£¬ºÍÍêÕûµÄÈÕÆÚ¸ñʽ¡£

import java.text.DateFormat;

import java.util.Date;

public class DateExample4 {

public static void main(String[] args) {

Date date = new Date();

DateFormat shortDateFormat = DateFormat.getDateTimeInstance(

DateFormat.SHORT, DateFormat.SHORT);

DateFormat mediumDateFormat = DateFormat.getDateTimeInstance(

DateFormat.MEDIUM, DateFormat.MEDIUM);

DateFormat longDateFormat = DateFormat.getDateTimeInstance(

DateFormat.LONG, DateFormat.LONG);

DateFormat fullDateFormat = DateFormat.getDateTimeInstance(

DateFormat.FULL, DateFormat.FULL);

System.out.println(shortDateFormat.format(date));

System.out.println(mediumDateFormat.format(date));

System.out.println(longDateFormat.format(date));

System.out.println(fullDateFormat.format(date));

}

}

×¢ÒâÎÒÃÇÔÚ¶Ô getDateTimeInstanceµÄÿ´Îµ÷ÓÃÖж¼´«µÝÁËÁ½¸öÖµ£ºµÚÒ»¸ö²ÎÊýÊÇÈÕÆÚ·ç¸ñ£¬¶øµÚ¶þ¸ö²ÎÊýÊÇʱ¼ä·ç¸ñ¡£ËüÃǶ¼ÊÇ»ù±¾Êý¾ÝÀàÐÍint(ÕûÐÍ)¡£¿¼Âǵ½¿É¶ÁÐÔ£¬ÎÒÃÇʹÓÃÁËDateFormat ÀàÌṩµÄ³£Á¿: SHORT£¬MEDIUM£¬LONG£¬ºÍ FULL¡£

ÔËÐÐÎÒÃǵÄÀý×Ó³ÌÐòµÄʱºò£¬Ëü½«Ïò±ê×¼Êä³öÉ豸Êä³öÏÂÃæµÄÄÚÈÝ:

9/29/01 8:44 PM

Sep 29£¬2001 8:44:45 PM

September 29£¬2001 8:44:45 PM EDT

Saturday£¬September 29£¬2001 8:44:45 PM EDT

Calendar Àà

ÎÒÃÇÏÖÔÚÒѾ­Äܹ»¸ñʽ»¯²¢´´½¨Ò»¸öÈÕÆÚ¶ÔÏóÁË£¬µ«ÊÇÎÒÃÇÈçºÎ²ÅÄÜÉèÖúͻñÈ¡ÈÕÆÚÊý¾ÝµÄÌض¨²¿·ÖÄØ£¬±ÈÈç˵Сʱ£¬ÈÕ£¬»òÕß·ÖÖÓ? ÎÒÃÇÓÖÈçºÎÔÚÈÕÆÚµÄÕâЩ²¿·Ö¼ÓÉÏ»òÕß¼õÈ¥ÖµÄØ? ´ð°¸ÊÇʹÓÃCalendar Àà¡£

¼ÙÉèÄãÏëÒªÉèÖ㬻ñÈ¡£¬ºÍ²Ù×ÝÒ»¸öÈÕÆÚ¶ÔÏóµÄ¸÷¸ö²¿·Ö£¬±È·½Ò»¸öÔµÄÒ»Ìì»òÕßÊÇÒ»¸öÐÇÆÚµÄÒ»Ì죬ΪÁËÑÝʾÕâ¸ö¹ý³Ì£¬ÎÒÃǽ«Ê¹ÓþßÌåµÄ×ÓÀà java.util.GregorianCalendar¡£ ¿¼ÂÇÏÂÃæµÄÀý×Ó£¬Ëü¼ÆËãµÃµ½ÏÂÃæµÄµÚÊ®¸öÐÇÆÚÎåÊÇ13ºÅ¡£

import java.util.GregorianCalendar;

import java.util.Date;

import java.text.DateFormat;

public class DateExample5 {

public static void main(String[] args) {

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);

// Create our Gregorian Calendar.

GregorianCalendar cal = new GregorianCalendar();

// Set the date and time of our calendar

// to the system´s date and time

cal.setTime(new Date());

System.out.println("System Date: " + dateFormat.format(cal.getTime()));

// Set the day of week to FRIDAY

cal.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.FRIDAY);

System.out.println("After Setting Day of Week to Friday: " +dateFormat.format(cal.getTime()));

int friday13Counter = 0;

while (friday13Counter <= 10) {

// Go to the next Friday by adding 7 days.

cal.add(GregorianCalendar.DAY_OF_MONTH£¬7);

// If the day of month is 13 we have

// another Friday the 13th.

if (cal.get(GregorianCalendar.DAY_OF_MONTH) == 13) { friday13Counter++;

System.out.println(dateFormat.format(cal.getTime()));

}

}

}

}

ÔÚÕâ¸öÀý×ÓÖÐÎÒÃÇ×÷ÁËÓÐȤµÄº¯Êýµ÷ÓÃ:

cal.set(GregorianCalendar.DAY_OF_WEEK,

GregorianCalendar.FRIDAY);

ºÍ:cal.add(GregorianCalendar.DAY_OF_MONTH£¬7);

set ·½·¨Äܹ»ÈÃÎÒÃÇͨ¹ý¼òµ¥µÄÉèÖÃÐÇÆÚÖеÄÄÄÒ»ÌìÕâ¸öÓòÀ´½«ÎÒÃǵÄʱ¼äµ÷ÕûΪÐÇÆÚÎå¡£×¢Òâµ½ÕâÀïÎÒÃÇʹÓÃÁ˳£Á¿ DAY_OF_WEEK ºÍ FRIDAYÀ´ÔöÇ¿´úÂëµÄ¿É¶ÁÐÔ¡£add ·½·¨ÈÃÎÒÃÇÄܹ»ÔÚÈÕÆÚÉϼÓÉÏÊýÖµ£¬ÈóÄêµÄËùÓи´ÔӵļÆË㶼ÓÉÕâ¸ö·½·¨×Ô¶¯´¦Àí¡£

ÎÒÃÇÕâ¸öÀý×ÓµÄÊä³ö½á¹ûÊÇ:

System Date: Saturday£¬September 29£¬2001

µ±ÎÒÃǽ«ËüÉèÖóÉÐÇÆÚÎåÒÔºó¾Í³ÉÁË:

Friday£¬September 28£¬2001

Friday£¬September 13£¬2002

Friday£¬December 13£¬2002

Friday£¬June 13£¬2003

Friday£¬February 13£¬2004

Friday£¬August 13£¬2004

Friday£¬May 13£¬2005

Friday£¬January 13£¬2006

Friday£¬October 13£¬2006

Friday£¬April 13£¬2007

Friday£¬July 13£¬2007

Friday£¬June 13£¬2008

ʱ¼äÕÆÎÕÔÚÄãµÄÊÖÀï

ÓÐÁËÕâЩDate ºÍCalendar ÀàµÄÀý×Ó£¬ÄãÓ¦¸ÃÄܹ»Ê¹Óà java.util.Date£¬java.text.SimpleDateFormat£¬ºÍ java.util.GregorianCalendar ´´½¨Ðí¶à·½·¨ÁË¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ