views:

75

answers:

2

Here i am getting one Map. That map contain the Date of birth detail of person as a GregorianCalendar. For example My Map is

{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} using

From this one i need to get an actual date of birth of that person. but In my database it is in date format only.That datatype of column in DB is datetime. I should not change this type. now how can i get the date of birth of that person in a date format using java

+3  A: 
Calendar calendar  = ( Calendar )  thatMap.get("dateOfBirth");
Date date = calendar.getTime();

Here's a sample you can use to test it, and see it does what you need.

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

    }
}

Output:

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=[]}

The date is: 26.08.2010

OscarRyz
I think that the cast is unnecessary, as `GregorianCalendar extends Calendar`
FarmBoy
@FarmBoy, yes, but the map defined returns `Object` so the cast is needed.
OscarRyz
Thank u very much.I got itI got th format u specified(Thu Aug 26 07:07:27 CDT 2010). but i need like 26.08.2010. is it possible to get predefined function. how can i get like that
Twity
@Twity, create a new question, for that. It would help others too. BTW, that's pretty easy ( hint: Google SimpleDateFormat ) **edit** I've added to the answer, the formatting part. Bear in mind that what you have is a **String** with the specified format, and **not** a date objects as you initially wanted.
OscarRyz
+2  A: 

From java.sql.Date to java.util.Calendar (or 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);

From java.util.Calendar to java.sql.Date

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

** NOTE **

java.sql.Timestamp is a sibling of java.sql.Date and both extends java.util.Date, therefore you can use either exactly the same way.

Also, to convert a date string into a date object, use 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);

And to reverse it

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