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 4 of 4

Thread: Date and SimpleDateFormat Class

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Date and SimpleDateFormat Class

    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class Time_Difference{

    public static void main(String[] args) {

    String dateStart = "01/15/2012 09:29:58";
    String dateStop = "01/15/2012 10:29:58";

    //HH converts hour in 24 hours format (0-23), day calculation
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

    Date d1 = null; //Doubt here...
    Date d2 = null;

    try {
    d1 = format.parse(dateStart);
    d2 = format.parse(dateStop);

    //in milliseconds
    long diff = d2.getTime() - d1.getTime();

    long diffSeconds = diff / 1000 % 60;
    long diffMinutes = diff / (60 * 1000) % 60;
    long diffHours = diff / (60 * 60 * 1000) % 24;
    long diffDays = diff / (24 * 60 * 60 * 1000);

    System.out.print(diffDays + " days, ");
    System.out.print(diffHours + " hours, ");
    System.out.print(diffMinutes + " minutes, ");
    System.out.print(diffSeconds + " seconds.");

    } catch (Exception e) {
    e.printStackTrace();
    }

    }




    I have 3 questions in the above code block

    1) why do we assign null to the Date variables d1 & d2?
    2)What exactly does does d1 and d2 return after the executing the following lines of code?
    d1 = format.parse(dateStart);
    d2 = format.parse(dateStop);
    I know we are converting date string to some integer value ...But what exactly is that value??

    3] please explain the below logic of how the diff between d1 & d2 is converted into seconds,minutes, hours etc..
    long diffSeconds = diff / 1000 % 60;
    long diffMinutes = diff / (60 * 1000) % 60;
    long diffHours = diff / (60 * 60 * 1000) % 24;
    long diffDays = diff / (24 * 60 * 60 * 1000);



    Thank you i really appreciate ur time and support....It means a lot for a beginner like me


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,810
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: Date and SimpleDateFormat Class

    You were given a link to the general forum guidelines in your first post that included instructions on how to post code correctly. Please review those instructions and post your code correctly with code or highlight tags.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Date and SimpleDateFormat Class

    Quote Originally Posted by Ryan7744 View Post
    1) why do we assign null to the Date variables d1 & d2?
    The initialization to null is not really necessary in this case, because d1/d2 are assigned by format.parse before any use of these variables.

    Quote Originally Posted by Ryan7744 View Post
    2)What exactly does does d1 and d2 return after the executing the following lines of code?
    d1 = format.parse(dateStart);
    d2 = format.parse(dateStop);
    I know we are converting date string to some integer value ...But what exactly is that value??
    The main concept behind Date class is that it contains 1 long value representing the number of milliseconds since the "epoch" (January 1, 1970, 00:00:00 GMT).

    Quote Originally Posted by Ryan7744 View Post
    3] please explain the below logic of how the diff between d1 & d2 is converted into seconds,minutes, hours etc..
    long diffSeconds = diff / 1000 % 60;
    long diffMinutes = diff / (60 * 1000) % 60;
    long diffHours = diff / (60 * 60 * 1000) % 24;
    long diffDays = diff / (24 * 60 * 60 * 1000);
    Since diff is a difference in milliseconds, it's easy to understand these calculations. For example, diffSeconds is from the milliseconds divided by 1000. However only /1000 would be the number of "absolute" seconds since epoch. So % 60 you have the seconds in that minute.
    The others are similar.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  4. #4
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Date and SimpleDateFormat Class

    Thank u andbin ... I understood the logic and the code as well .. Thanks a lot

Similar Threads

  1. Controller for date class
    By Ecosse in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 2nd, 2012, 10:21 AM
  2. Building your own Date class.
    By ShaunB in forum Java SE API Tutorials
    Replies: 5
    Last Post: October 25th, 2011, 05:37 PM
  3. DateFormat and SimpleDateFormat
    By JDogRob in forum Java SE APIs
    Replies: 4
    Last Post: October 12th, 2011, 08:51 PM
  4. SimpleDateFormat
    By smrtalex in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 7th, 2010, 09:44 PM
  5. Replies: 3
    Last Post: May 8th, 2009, 01:27 PM