Results 1 to 3 of 3

Thread: Setting a Date?

  1. #1
    Zachafer is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Setting a Date?

    Hey guys. Trying to convert a .NET project to Java here. Here's basically what my program does:

    Java Code:
    Date old = new Date("2/23/2011 12:04:53 PM");
    Date now = Date.getNow();
    while(old < now)
        old = old.addMinutes(7);
    So basically, I have a date/time stored into a variable, and I want to keep adding 7 minutes until that date/time is in the future (past current time).

    Any help is greatly appreciated.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Calendar has a set(int field,int value) method which will allow you to update Calendar.MINUTE by whatever the value specified. Date has boolean methods for comparing two Date's i.e. before(Date d) and after(Date d). This should be enough to point you in the right direction

  3. #3
    Zachafer is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Here's my C# code:
    Java Code:
                DateTime next = DateTime.Parse("2/23/2011 12:04:53 PM");
                while (next < DateTime.Now)
                    next = next.AddMinutes(7);
                Console.Write("Next time: " + next);
    I need someone to convert this to Java.

    Edit: Managed to get something working:
    Java Code:
        private static Date getDate() throws ParseException {
            DateFormat format = new SimpleDateFormat("MM/dd/yyyy h:m:s a");
            Date next = format.parse("2/23/2011 12:04:53 PM");
            while(next.before(Calendar.getInstance().getTime()))
                next.setTime(next.getTime() + 420000);
            return next;
        }
    If it can be improved, please tell me!
    Last edited by Zachafer; 02-24-2011 at 09:49 PM.

Similar Threads

  1. converting string (GMT date) to date in US time.
    By JRuyechan in forum New To Java
    Replies: 1
    Last Post: 10-15-2010, 07:07 AM
  2. julian date to full date format
    By judy318 in forum New To Java
    Replies: 7
    Last Post: 11-02-2009, 12:17 PM
  3. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 08:10 AM
  4. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 08:32 PM
  5. Difference between current date and anothe date
    By vijay balusamy in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 04:15 PM

Posting Permissions

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