Trouble with Date & Calendar?

Discussion in 'Java' started by harry, Jun 7, 2006.

  1. harry

    harry Guest

    I want to create a Date object that will always represent tomorrow @ 04:00am
    but am a bit confused on the easiest way to do this?

    So if now = 7th June, 2006 @ 18:30:00 I want to create a Date object for the
    8th June, 2006 @ 04:00:00 if you see what I mean?

    thanks in advance

    harry
    harry, Jun 7, 2006
    #1
    1. Advertising

  2. harry

    Guest

    import java.text.FieldPosition;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;

    /**
    * Code to illustrate adding a DAY and setting fixed hour
    * and minute.
    *
    * @author Sai Matam (saimatam at yahoo dot com )
    *
    */

    public class NextDay4Main
    {
    public static SimpleDateFormat
    sdf = new SimpleDateFormat("MM/dd/yy HH:mm:ss");

    public static void main(String[] args)
    {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(new Date());
    System.out.println(" Current date is: " + formatDate(cal.getTime()));
    cal.add(Calendar.DAY_OF_YEAR, 1);
    cal.set(Calendar.HOUR_OF_DAY, 4);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    System.out.println("Required date is: " + formatDate(cal.getTime()));
    }

    public static String formatDate(Date inDate)
    {
    StringBuffer buf = new StringBuffer(1024);
    sdf.format(inDate, buf, new FieldPosition(0) );
    return buf.toString();
    }
    }


    harry wrote:
    > I want to create a Date object that will always represent tomorrow @ 04:00am
    > but am a bit confused on the easiest way to do this?
    >
    > So if now = 7th June, 2006 @ 18:30:00 I want to create a Date object for the
    > 8th June, 2006 @ 04:00:00 if you see what I mean?
    >
    > thanks in advance
    >
    > harry
    , Jun 7, 2006
    #2
    1. Advertising

  3. "harry" <> wrote in message
    news:uaDhg.82385$...
    >I want to create a Date object that will always represent tomorrow @
    >04:00am
    > but am a bit confused on the easiest way to do this?
    >
    > So if now = 7th June, 2006 @ 18:30:00 I want to create a Date object for
    > the
    > 8th June, 2006 @ 04:00:00 if you see what I mean?
    >
    > thanks in advance


    What you want to do is to get the current date, roll the day over to the
    next day and then set the timestamp to your time. Something like this:

    public class Anon1 {
    static SimpleDateFormat sdf = new SimpleDateFormat ("MM-dd-yyyy
    HH:mm:ss.SSSS");

    public static final void main (String [] args) {
    Calendar cal = GregorianCalendar.getInstance ();
    System.out.println(sdf.format(cal.getTime()));

    cal.add(Calendar.DATE, 1);

    cal.set(Calendar.HOUR_OF_DAY, 4);
    cal.set(Calendar.MINUTE, 30);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND,0);

    System.out.println (sdf.format(cal.getTime()));
    }
    }

    Cheers,
    Matt Humphrey http://www.iviz.com/
    Matt Humphrey, Jun 7, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    587
    Matthew Speed
    Nov 8, 2003
  2. Shevek
    Replies:
    3
    Views:
    6,011
    Shevek
    Jun 23, 2004
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,226
    Michael Borgwardt
    May 30, 2004
  4. Shevek
    Replies:
    0
    Views:
    373
    Shevek
    Jun 23, 2004
  5. Replies:
    0
    Views:
    190
Loading...

Share This Page