Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Trouble with Date & Calendar?

Thread view: 
harry - 07 Jun 2006 17:30 GMT
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
saimatam@gmail.com - 07 Jun 2006 20:00 GMT
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();
    }
}

> 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?
[quoted text clipped - 5 lines]
>
> harry
Matt Humphrey - 07 Jun 2006 20:00 GMT
>I want to create a Date object that will always represent tomorrow @
>04:00am
[quoted text clipped - 5 lines]
>
> 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 matth@ivizNOSPAM.com http://www.iviz.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.