我如何从日历中获取日期?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (20)

我有一个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格式的生日?

提问于
用户回答回答于

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());

用户回答回答于
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=[]
    }

扫码关注云+社区