CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2000
    Posts
    6

    Converting util.Date to util.Calendar

    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.



  2. #2
    Join Date
    Jan 2000
    Location
    Canada
    Posts
    249

    Re: Converting util.Date to util.Calendar

    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

  3. #3
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Converting util.Date to util.Calendar


    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..


  4. #4
    Join Date
    May 2000
    Location
    JiangSu,China
    Posts
    1

    Re: Converting util.Date to util.Calendar

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured