Click to See Complete Forum and Search --> : Converting util.Date to util.Calendar
dira
May 9th, 2000, 08:52 AM
I've read a date from a database (java.sql.Date).
How can I convert this type to java.util.Calendar
or java.util.GregorianCalendar in JDK 1.2?
The getTime()-Method of java.util.Date is the only Method that is not deprecated, but the Method setTimeInMilis(long) in java.util.Calendar is protected.
weaver
May 9th, 2000, 09:38 AM
Ok, say you have an instance of java.sql.Date called date and an instance of java.util.Calendar called calendar. Use the following:
calendar = new java.util.Calendar();
calendar.set(date.getYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes() );
That should work.
-------------------------------------------
weaver
icq# 64665116
Please rate this post.
http://weaver.x7.htmlplanet.com
poochi
May 9th, 2000, 10:53 AM
There is a method called "setTime()" in Calendar class which takes Date as an Input.
public final void setTime(Date date)
Calendar cal = Calendar.getInstance();
cal.setTime( tYourDateObject );
Poochi..
Summer Tao
May 10th, 2000, 07:45 PM
AHa!
i think the best way is to use Canlendar's protect method:complete().
For example:
Suppose you have date object. Year,month,day value is available.
You can create a class derived from Calendar(or GregorianCalendar as you like),
In this subclass's construct body,you can use this.complete to set the fields that can be caculated.
see:
class MyCalClass extend Calendar {
.....................
public MyCalClass(int year,int month,int date) {
super(year,month,date);
this.complete();
}
.....................
}
then you can use:
MySubClass someOject(year,month,date);
someObject.get(MySubClass.MILLISECOND);
someObject.get(MySubClass.DayOfWeek);
...........................
you can get value of millisecond,weekOfMonth,weekOfYear,dayOfMonth,dayOfYear,etc.
a lovely man,like prgramming with java.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.