Results 1 to 8 of 8
  1. #1
    kryptonian03 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default Strings as Dates and difference

    Good day guys!

    If i have a string lets say "8:00AM" and a "1:00PM" how can i set the string as time can compute for a difference which is 3 hours...

    Thank you very much!!!

  2. #2
    vitthal is offline Member
    Join Date
    Mar 2011
    Location
    Pune, India
    Posts
    5
    Rep Power
    0

    Default

    This Example may help you!
    Set time whatever you want!
    Java Code:
    import java.text.SimpleDateFormat;  
    import java.util.Calendar;  
    import java.util.Date;  
    import java.util.TimeZone;  
      
    public class DateTest {  
      
    public class DateTest {  
      
    static SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");  
      
    public static void main(String[] args) {  
      
      TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));  
      
      //diff between these 2 dates should be 1  
      Date d1 = new Date("01/01/2007 12:00:00");  
      Date d2 = new Date("01/02/2007 12:00:00");  
      
      //diff between these 2 dates should be 1  
      Date d3 = new Date("03/24/2007 12:00:00");  
      Date d4 = new Date("03/25/2007 12:00:00");  
      
      Calendar cal1 = Calendar.getInstance();cal1.setTime(d1);  
      Calendar cal2 = Calendar.getInstance();cal2.setTime(d2);  
      Calendar cal3 = Calendar.getInstance();cal3.setTime(d3);  
      Calendar cal4 = Calendar.getInstance();cal4.setTime(d4);  
      
      printOutput("Manual   ", d1, d2, calculateDays(d1, d2));  
      printOutput("Calendar ", d1, d2, daysBetween(cal1, cal2));  
      System.out.println("---");  
      printOutput("Manual   ", d3, d4, calculateDays(d3, d4));  
      printOutput("Calendar ", d3, d4, daysBetween(cal3, cal4));  
    }  
      
      
    private static void printOutput(String type, Date d1, Date d2, long result) {  
      System.out.println(type+ "- Days between: " + sdf.format(d1)  
                        + " and " + sdf.format(d2) + " is: " + result);  
    }  
      
    /** Manual Method - YIELDS INCORRECT RESULTS - DO NOT USE**/  
    /* This method is used to find the no of days between the given dates */  
    public static long calculateDays(Date dateEarly, Date dateLater) {  
      return (dateLater.getTime() - dateEarly.getTime()) / (24 * 60 * 60 * 1000);  
    }  
      
    /** Using Calendar - THE CORRECT WAY**/  
    public static long daysBetween(Calendar startDate, Calendar endDate) {  
      Calendar date = (Calendar) startDate.clone();  
      long daysBetween = 0;  
      while (date.before(endDate)) {  
        date.add(Calendar.DAY_OF_MONTH, 1);  
        daysBetween++;  
      }  
      return daysBetween;  
    }  
    }
    OUTPUT:
    Manual - Days between: 01-Jan-2007 and 02-Jan-2007 is: 1
    Calendar - Days between: 01-Jan-2007 and 02-Jan-2007 is: 1
    ---
    Manual - Days between: 24-Mar-2007 and 25-Mar-2007 is: 0
    Calendar - Days between: 24-Mar-2007 and 25-Mar-2007 is: 1

  3. #3
    kryptonian03 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    Thanks for the post... but what i want is to input two times (eg. 9:16AM and 1:30PM)... What shall i do to get the date difference of hours and minutes?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    kryptonian03 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    I have tried using spilt which splits up time in and time out from ":" ... WHich means time in will produce 9 and 16 and the time out will have 1 and 30... but it seems its not right in finding out the difference here... What shall i do?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #7
    kryptonian03 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    Im sorry about not posting the code, here it is:
    Java Code:
    import java.util.*;
    import java.io.*;
    public class Splitter{
    	public static void main (String args []){
    		Scanner input = new Scanner(System.in);
    		String timein;
    		String timeout;
    		
    		System.out.println("Use the format hh:mm in entering of data: ");
    		System.out.println("Log-in: ");
    		timein = input.nextLine();
    		System.out.println("Log-out: ");
    		timeout = input.nextLine();		
    		String[] myStringArrayin= timein.split(":");
    		String[] myStringArrayout= timeout.split(":");
    		int[] myTimein = new int[myStringArrayin.length];
    		int[] myTimeout = new int[myStringArrayin.length];
    		
    		for (int i = 0; i < myStringArrayin.length; i++) {
    			myTimein[i] = Integer.parseInt(myStringArrayin[i]);	}
    		System.out.println();
    		for (int i = 0; i < myStringArrayout.length; i++) {
    			myTimeout[i] = Integer.parseInt(myStringArrayout[i]);	}
    		
    		System.out.println(myTimein[0] + "   " + myTimein[1]);
    		System.out.println(myTimeout[0] + "   " + myTimeout[1]);
    		
    		int [] myTimeDiff =  new int [2];
    		myTimeDiff[0]=myTimeout[0] - myTimein[0];
    		myTimeDiff[1]=myTimeout[1] - myTimein[1];
    		
    		
    		System.out.println(myTimeDiff[0] + "   " + myTimeDiff[1]);
    
    
    	}
    }
    It splits up time in and time out and can display the difference but it needs a little bit of tweaking specially on time difference on minutes... Thanks a lot

  8. #8
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    You can do this without touching your initial input times. Just parse it as a date/time string (adding some common date of course):

    Java Code:
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd hh:mmaa");
        String syyyyMMdd = "20110320";
        String sTime1 = "8:00AM";
        String sTime2 = "1:00PM";
    
        try {
            Date date1 = format.parse(syyyyMMdd + " " + sTime1);
            Date date2 = format.parse(syyyyMMdd + " " + sTime2);
            long lSeconds = (date2.getTime() - date1.getTime())/1000;
            System.out.println("difference in seconds:  " + lSeconds);
            System.out.println("difference in minutes:  " + lSeconds/60);
            System.out.println("  difference in hours:  " + lSeconds/(3600));
        } catch (ParseException e) {
            e.printStackTrace();
        }

Similar Threads

  1. Dates sometimes off by a day?
    By saggio in forum Advanced Java
    Replies: 13
    Last Post: 12-08-2010, 05:56 PM
  2. UTC Dates
    By PedroCosta in forum Advanced Java
    Replies: 3
    Last Post: 04-01-2010, 07:08 PM
  3. Difference of Two Dates
    By jboy in forum New To Java
    Replies: 3
    Last Post: 09-15-2009, 07:41 PM
  4. Replies: 23
    Last Post: 02-19-2009, 06:24 AM
  5. Help with dates
    By bumblyb33 in forum New To Java
    Replies: 1
    Last Post: 02-13-2009, 03:54 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
  •