¡¡¡¡ÓÉÓÚ¹«Ë¾ÏîÄ¿µÄÐèÒª£¬±¾ÈËÌØÒâ¶ÔJavaÖд¦ÀíÈÕÆÚµÄÀàºÍ·½·¨Ñо¿ÁËһϣ¬ÏÖ×ܽáÈçÏ£¬¹©ÅóÓѲο¼£¬ÓÐʲô²»Í׵ĵط½»¶ÓÖ¸Õý¡£
JavaÖд¦ÀíÈÕÆÚµÄÀàÖ÷ÒªÓÐDate¡¢CalendarºÍDateFormatÈý¸ö£¬ÔÚJDKÖÐËüÃǶÔÓ¦µÄ°üµÄλÖúÍÏà¹ØµÄÅÉÉúÀàÈçÏÂËùʾ£º
1¡¢ Date£º¾ßÌåÀàjava.util.Date £»
2¡¢ Calendar:³éÏóÀàjava.text.DateFormat,ËüÅÉÉúµÄÒ»¸ö×ÓÀàΪjava.text.SimpleDateFormat
3¡¢ DateFormat:³éÏóÀàjava.util.Calendar£¬ËüÅÉÉúµÄÒ»¸ö×ÓÀàΪjava.util.GregorianCalendar
£¨×¢£ºÈç¹ûÓÐÅóÓѶԾßÌåÀàºÍ³éÏóÀ಻Á˽⣬¿ÉÒԲο¼Ïà¹ØJava×ÊÁÏ£©
ÏÖÔÚ¶ÔÒÔÉÏÈý¸öÀà½øÐÐÈçÏ·ÖÎö£º
Ò»¡¢¶ÔDateÀàµÄ·ÖÎö
Date Àà´ÓJDK 1.0¿ªÊ¼½ø»¯£¬ µ±Ê±ËüÖ»°üº¬Á˼¸¸ö¼òµ¥µÄ´¦ÀíÈÕÆÚÊý¾ÝµÄ·½·¨¡£ ÓÉÓÚÕâЩ·½·¨ÊµÓÃÐԲÏÖÔÚ»ù±¾Éϱ»CalendarÀàÖи÷·½·¨Ëù´úÌæÁË¡£ÕâÖָĽøÄ¿µÄÊÇΪÁ˸üºÃµÄ´¦ÀíÈÕÆÚÊý¾Ý¹ú¼Ê»¯¸ñʽ¡£ Date Ààʵ¼ÊÉÏÖ»ÊÇÒ»¸ö°ü¹üÀà, Ëü°üº¬Ò»¸ö³¤ÕûÐÍÊý¾Ý, ±íʾµÄÊÇ´ÓGMT(¸ñÁÖÄáÖαê׼ʱ¼ä)1970Äê, 1 Ô 1ÈÕ00:00:00ÕâÒ»¿Ì֮ǰ»òÕßÊÇÖ®ºó¾ÀúµÄºÁÃëÊý.
ΪÁ˼ÓÉî¶ÔDateÀàµÄÀí½â£¬ÁоÙÈçÏÂÒ»¸ö¼òµ¥Àý×ÓÀ´ËµÃ÷DateµÄʹÓãº
[code]import java.util.Date;
public class DateTest1 {
public static void main(String[] args) {
/** Get the system Date **/
Date date = new Date();
System.out.println(date.getTime());
}
}[/code]
ϵͳÊä³öÈçϽá¹û£º
[code]1001803809710[/code]
ÔÚÕâ¸öÀý×ÓÖÐ,ÖµµÃÎÒÃÇ×¢ÒâµÄÊÇÎÒÃÇʹÓÃÁËDate ¹¹Ô캯Êý´´½¨Ò»¸öÈÕÆÚ¶ÔÏó, ÔÚûÓÐÈκβÎÊýµÄÇé¿öÏ£¬Õâ¸ö¹¹Ô캯Êý»á×Ô¶¯µ÷ÓÃSystem.currentTimeMillis() ·½·¨À´»ñÈ¡µ±Ç°ÏµÍ³ÈÕÆÚ.¡£
¶þ¡¢¶ÔDateFormatÀàµÄ·ÖÎö
Èç¹ûÎÒÃÇÏ£Íû¶¨ÖÆÈÕÆÚÊý¾ÝµÄ¸ñʽ, ±ÈÈçÏÔʾµÄÈÕÆÚ¸ñʽΪ£º2006Äê06ÔÂ23ÈÕ£¬³éÏóÀàjava.util.CalendarºÍËüµÄ×ÓÀàjava.util.GregorianCalendarÄÜÍê³ÉÕâÒ»¹¦ÄÜ¡£
ÏÂÃæµÄÀý×ÓչʾÁËÈçºÎÍê³ÉÕâ¸ö¹¤×÷:
[code]import java.util.SimpleDateFormat;
import java.util.Date;
public class DateTest2 {
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("yyyyÄêMMÔÂddÈÕ",Locale.US);
Date date = new Date();
System.out.println(bartDateFormat.format(date));
}
}[/code]
ֻҪͨ¹ýÏòSimpleDateFormat µÄ¹¹Ô캯Êý´«µÝ¸ñʽ×Ö·û´®" yyyyÄêMMÔÂddÈÕ", ÎÒÃǾͿÉÒÔÖ¸Ã÷×Ô¼ºÏëÒªµÄ¸ñʽ. ÆäÖÐyyyyÊÇÄ꣬MMÊÇÔÂ, ddÊÇÈÕ. ×Ö·ûµÄ¸öÊý¾ö¶¨ÁËÈÕÆÚÊÇÈçºÎ¸ñʽ»¯µÄ£¬Èç¹û¸ñʽΪ"yyyyÄêMMÔÂddÈÕ"¾Í»áÏÔʾ 06Äê06ÔÂ23ÈÕ£»Èç¹ûÊÇ"yy-MM-dd"¾ÍÏÔʾ06-06-23¡£
Èý¡¢¶ÔCalendar ÀàµÄ·ÖÎö
Èç¹ûÏëÉèÖᢻñÈ¡¡¢²Ù×ÝÒ»¸öÈÕÆÚ¶ÔÏóµÄ¸÷¸öÌض¨²¿·Ö£¬±ÈÈç»ñµÃСʱ, ÈÕ, ·ÖÖÓ¡¢¼ÆËãÒ»¸öÔµÄijһÌì˵ÐÇÆÚ¼¸µÈµÈ¡£ÕâÑùµÄÇé¿ö¾ÍÐèÒª³éÏóÀàjava.util.CalendarºÍËüµÄ×ÓÀàÀ´´¦Àí¡£ÎªÁËÄÜÇå³þµØ˵Ã÷ÎÊÌ⣬ÁоÙÏÂÃæµÄÀý×Ó, ËüµÄ¹¦ÄÜÊǼÆËãµÚÊ®¸öÐÇÆÚÎåÊÇ13ºÅ.
[code]import java.util.GregorianCalendar;
import java.util.Date;
import java.text.DateFormat;
public class CalendarTest{
public static void main(String[] args) {
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
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()));
}
}
}
}[/code]
Êä³ö½á¹ûÊÇ:
[code]System Date: Saturday, September 29, 2005[/code]
ËÄ¡¢×ܽá
ÔÚJavaͨ¹ýÒ»ÏÂÈý¸öÀàµÄʹÓã¬Ò»°ã¿ÉÒÔÂú×ã¸÷ÖÖÐÎʽµÄÈÕÆÚʹÓá£Êµ¼ÊÉÏ£¬JavaÖÐÈÕÆÚʹÓõķ½·¨»¹Óкܶ࣬Èç¹ûÓÐÐËȤµÄÅóÓÑÇë²Î¿¼JDK°ïÖúÊֲᡣ
¹ØÓÚ×÷Õߣº
ÑîÏþ£¬¼ÆËã»ú˶ʿ£¬ÔøÔÚNEC-SI¡¢Softbrain µ£ÈÎjava¿ª·¢¹¤×÷¡£