Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to convert to/from calendars to/from dates.

807580Oct 5 2009 — edited Oct 6 2009
Can someone help me make this UT pass? It worked for a while and seems to fail on line 51 with random values for 'h". I think it is a time zone problem.

I'm having a similar problem with saving and retrieving java.sql.timestamp to a database via spring/hiberante/dbunit. I hope the issue is the same.
package com.SIGNITEK.demo;

import static org.junit.Assert.assertEquals;

import static org.junit.Assert.assertTrue;

import java.io.PrintStream;

import java.util.Calendar;

import java.util.Date;

import java.util.TimeZone;

import org.junit.Before;

import org.junit.Test;


/**

 * @author siegfried heintze

 *

 */

public class CalendarDatesTest {

    static final PrintStream out = System.out;


    // Define sample date-time

    public final static int nowYear = 2009;

    public final static int nowMonth = 7;

    public final static int nowDayOfMonth = 11;

    public final static int nowHourOfDay = 15;

    public final static int nowMinute = 34;

    public final static int nowSecond = 52;

    public final static int nowMilliSecond=0;

    public final static int nowZONE_OFFSET=-28800000;

    public final static int nowDST_OFFSET=3600000;

    public final static int nowAM_PM=1;

    public final static int nowHOUR=2;

    public final static TimeZone nowTimeZone = TimeZone.getTimeZone("America/Los_Angeles");

    long tstOldMillis=0;

    

/* (non-Javadoc)

 * @see com.amazon.associates.platform.testutils.db.spring.hibernate.AbstractDBUnitTestCase#setUp()

 */

@Before

  public void setUp() {}


  // Calendars are still a mystery and this test still fails.

  @Test

    public void testGregDates(){

    Date t = GregorianDate.make(2009, 9, 11, 15, 34);

    GregorianDate jd = new GregorianDate(t);

    int y = jd.getYear(), mo = jd.getMonth(),d=jd.getDay(),h=jd.getHour();

    int m=jd.getMinute();

    int s=jd.getSecond();

    assertEquals(2009,y);

    assertEquals(10,mo);

    assertEquals(11,d);

    out.println("h ="+h);

    assertTrue(15==h || h==17);  // this is too weird!

    assertEquals(34,m);

  }

}  

/**

 * Make date comparisons easier by providing a class 

 * that accepts year, month, day, hour, minutes, seconds as constructor arguments.

 * 

 * @author heintze

 *

 */

class GregorianDate {

  private int year, month, day, hour, minute, second;

  public int getYear() { return year; }

  public int getMonth() { return month; }

  public int getDay() { return day; }

  public int getHour() { return hour; }

  public int getMinute() { return minute; }

  public int getSecond() { return second; }

  /**

   * Make a Date.

   * @param year

   * @param month

   * @param dayOfMonth

   * @param hour

   * @param minute

   * @param second

   * @return

   */

  public static Date make(int year, int month, int dayOfMonth, int hour, int minute, int second){ 

    Calendar cal = Calendar.getInstance(CalendarDatesTest.nowTimeZone);  

    cal.set(year, month, dayOfMonth, hour, minute, second);

    //cal.setTimeZone(TimeZone.getDefault());

    cal.set(Calendar.DST_OFFSET, CalendarDatesTest.nowDST_OFFSET);

    cal.set(Calendar.ZONE_OFFSET, CalendarDatesTest.nowZONE_OFFSET);

    cal.set(Calendar.AM_PM, CalendarDatesTest.nowAM_PM);

    //cal.set(Calendar.HOUR, MessageDaoTest.nowHOUR);               

    Date date = new Date(cal.getTimeInMillis());

    return date;

  }

  public static Date make(int year, int month, int dayOfMonth, int hour, int min){ 

    return make(year,month,dayOfMonth, hour, min,0);

  }

  /**

   * Extract year, month, day, hour, minute, second from Date.

   * @param date

   */

  public GregorianDate(Date date){

    Calendar cal = Calendar.getInstance(CalendarDatesTest.nowTimeZone);  

    cal.setTime(date);

    //cal.setTimeZone(TimeZone.getDefault());

    year = cal.get(Calendar.YEAR);

    month = cal.get(Calendar.MONTH)+1;

    day = cal.get(Calendar.DAY_OF_MONTH);

    hour = cal.get(Calendar.HOUR_OF_DAY); // do not use .HOUR!

    minute = cal.get(Calendar.MINUTE);

    second = cal.get(Calendar.SECOND);

  }

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 3 2009
Added on Oct 5 2009
3 comments
465 views