2010-08-26 195 views
17

我有一個Map包含一個人的生日作爲GregorianCalendar如何從日曆中獲取日期?

例如:

{ 
    motherEmailID=null, 
    coreType=Ticket, 
    _NULL=null, 
    additionalFaclitiesProvided=[], 
    dateOfBirth=java.util.GregorianCalendar[ 
     time=585340200000, 
     areFieldsSet=false, 
     areAllFieldsSet=false, 
     lenient=true, 
     zone=sun.util.calendar.ZoneInfo[id="GMT", 
    offset=0, 
    dstSavings=0, 
    useDaylight=false, 
    transitions=0, 
    lastRule=null], 
     firstDayOfWeek=1, 
     minimalDaysInFirstWeek=1, 
     ERA=1, 
     YEAR=1988, 
     MONTH=6, 
     WEEK_OF_YEAR=30, 
     WEEK_OF_MONTH=4, 
     DAY_OF_MONTH=20, 
     DAY_OF_YEAR=202, 
     DAY_OF_WEEK=4, 
     DAY_OF_WEEK_IN_MONTH=3, 
     AM_PM=0, 
     HOUR=0, 
     HOUR_OF_DAY=0, 
     MINUTE=0, 
     SECOND=0, 
     MILLISECOND=0, 
     ZONE_OFFSET=19800000, 
     DST_OFFSET=0], 
    targetEnd=null, 
    year_semester=null 
} 

我需要一個Date,但在我的數據庫是隻Calendar格式。 數據庫中列的數據類型爲DateTime。 如何獲取Date格式的生日?

+1

請格式化你的代碼的可讀性,你可以把代碼標記或者在代碼使用'他們周圍的字符 – 2010-08-26 11:59:48

+0

格式它,使其可讀的,請張貼代碼。 – 2010-08-26 11:59:58

回答

28
Calendar calendar = (Calendar) thatMap.get("dateOfBirth"); 
Date date = calendar.getTime(); 

這裏是你可以用它來測試它,看看它做你所需要的樣本。

import java.util.*; 
import java.text.*; 

public class GetDate { 
    public static void main(String [] args) { 
     Map map = new HashMap(); 
     map.put("dateOfBirth", Calendar.getInstance()); 
     map.put("additionalFaclitiesProvided", new ArrayList()); 
     /// etc. 
     System.out.println(map); 

     Calendar cal = (Calendar) map.get("dateOfBirth"); 
     Date date = cal.getTime(); 
     // Addressing your comment: 
     SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); 
     System.out.println("The date is: "+ sdf.format(date) ); 
    } 
} 

輸出:

java GetDate 
    {dateOfBirth=java.util.GregorianCalendar[ 
     time=1282824447050,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[ 
      id="America/Mexico_City",offset=-21600000,dstSavings=3600000,useDaylight=true,transitions=99,lastRule=java.util.SimpleTimeZone[ 
       id=America/Mexico_City,offset=-21600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=3,startDay=1, 
       startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=7200000,endTimeMode=0 
      ] 
     ],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2010,MONTH=7,WEEK_OF_YEAR=35,WEEK_OF_MONTH=4,DAY_OF_MONTH=26,DAY_OF_YEAR=238,DAY_OF_WEEK=5, 
      DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=7,HOUR_OF_DAY=7,MINUTE=7,SECOND=27,MILLISECOND=50,ZONE_OFFSET=-21600000,DST_OFFSET=3600000]**, additionalFaclitiesProvided=[] 
    } 

日期爲:2010年8月26日

+0

我認爲演員是不必要的,因爲'GregorianCalendar延伸日曆' – 2010-08-26 12:06:51

+1

@FarmBoy,是的,但是定義的地圖返回'Object',所以需要演員陣容。 – OscarRyz 2010-08-26 12:08:18

+0

非常感謝。我得到了它 我得到了指定的th格式(Thu Aug 26 07:07:27 CDT 2010)。但我需要26.08.2010。是否有可能獲得預定義的功能。我怎麼能得到這樣的結果 – Twity 2010-08-26 12:39:22

5

java.sql.Datejava.util.Calendar(或java.util.GregorianCalendar

Calendar cal = new GregorianCalendar(); 
cal.setTime(date); // java.sql.Date date; 

// then set the GregorianCalendar in your map 
map.put('dateOfBirth', cal); 

java.util.Calendarjava.sql.Date

java.sql.Date date = new java.sql.Date(map.get('dateOfBirth').getTimeInMillis()); 

** 注意 **

java.sql.Timestampjava.sql.Date兄弟和兩個延伸java.util.Date,因此您可以使用完全相同的方式。

此外,爲日期字符串轉換成日期對象,用SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); 
Date d = sdf.parse("2010-08-26 8:34:00"); 
Calendar cal = Calendar.getInstance(); 
cal.setTime(d); 

,並扭轉其

String dateStr1 = sdf.format(cal.getTime()); 
// or 
String dateStr2 = sdf.format(date); // java.sql.Date/java.util.Date 
1

複製並運行該示例。

只需更改模式以測試不同的開關條件。

對於封裝只是使私人的幾個方法。日期格式不是您要做的工作,因爲方法現在正在返回日期,並且由調用者使用他想要的任何格式。

import java.util.Date; 
import java.util.GregorianCalendar; 

public class RandomDateOfBirthGenerator { 

    private static GregorianCalendar gc = new GregorianCalendar(); 

    public static enum Mode { 
     SENIOR, ADULT, YOUTH, CHILD 
    } 

    public static Date generateRandomDateOfBirth(Mode mode) { 

     int year = 0; 

     switch(mode){ 
     case SENIOR: 
      year = randBetween(1900, 1940); 
      break; 

     case ADULT: 
      year = randBetween(1941, 1995); 
      break; 

     case YOUTH: 
      year = randBetween(1995, 2002); 
      break; 

     case CHILD: 
      year = randBetween(2002, 2014); 
      break; 
     } 

     gc.set(gc.YEAR, year); 

     int dayOfYear = randBetween(1, gc.getActualMaximum(gc.DAY_OF_YEAR)); 

     gc.set(gc.DAY_OF_YEAR, dayOfYear); 

     return gc.getTime(); 
    } 

    private static int randBetween(int start, int end) { 
     return start + (int) Math.round(Math.random() * (end - start)); 
    } 

    public static void main(String[] args) { 
     System.out.println(generateRandomDateOfBirth(Mode.CHILD)); 
    } 
} 
相關問題