Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with Date.getTime()

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Date.getTime()

    Hi,

    I'm having a problem with this method and I can't see what I'm doing wrong

    import java.util.Date;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.text.ParseException;
     
    public class Main {
     
        public static void main(String[] args) throws ParseException {
     
        DateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
        String start = "19-09-2011  08:00:00";
        String end = "19-09-2011  12:00:00";
     
        Date d = format.parse(start);
        System.out.println(d.getTime());
        d = format.parse(end);
        System.out.println(d.getTime());
        }
    }

    When I use that code, I get that end < start but only if end is set at 12:00:00. For example, if I change the ending hour to 11:00:00 it works fine. Any ideas?

    Thanks!

  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Help with Date.getTime()

    I honestly had no idea what was going on, as I've never used these classes before.
    I read the API for SimpleDateFormat: SimpleDateFormat (Java 2 Platform SE v1.4.2) and noticed their examples were using
    capital letters for the hour. So by changing hh:mm:ss to HH:mm:ss it worked fine.

    I cannot tell you why, but I would be grateful If you could go away and research it yourself, before posting what you find out here for others who stumble across this thread.
    My only guess would be that capital letters might have some relation to a 24 hour format.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Date.getTime()

    I changed hh:mm:ss to HH:mm:ss and it worked fine. I looked at what was the cause and it seems 'h' is used for the am/pm hour format and 'H' for the 24-hour format.

    Thanks newbie.

Similar Threads

  1. Replies: 1
    Last Post: July 22nd, 2011, 07:08 AM
  2. Date help
    By banny7 in forum AWT / Java Swing
    Replies: 4
    Last Post: July 18th, 2011, 10:41 AM
  3. same date should entered in another date field
    By shashib09 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 14th, 2011, 08:42 AM
  4. Date problem
    By java_wannabe in forum Java Theory & Questions
    Replies: 7
    Last Post: April 22nd, 2011, 09:25 AM
  5. sql date problem
    By realosso in forum JDBC & Databases
    Replies: 2
    Last Post: June 3rd, 2010, 08:32 AM