1

Question 1: I have 2 fields to let user enter start date and end date, but in string format - DateStart (string: yyyy/mm/dd hh:mm) - DateEnd (string: yyyy/mm/dd hh:mm)

May I how to compare both datetime? I want to know total how many hours is difference between the both date.

Question 2: user will enter 1 returnDate (string: yyyy/mm/dd hh:mm) also in string format, may I know how to update the returnDate if I will need to add 55hours on the returnDate?

Thanks

user3522363
  • 35
  • 2
  • 8
  • 1
    Did you try with DateTime's functions of Java? – Rong Nguyen Apr 11 '14 at 06:41
  • 1
    For Q1; step 1, parse, step 2 compare, step 3 profit –  Apr 11 '14 at 06:41
  • What time zone? The difference in hours depends on the time zone because of Daylight Saving Time and other anomalies. – Basil Bourque Apr 11 '14 at 07:31
  • You are asking 3 questions rather than one: (1) How to parse a string into a date-time object in Java, (2) How to calculate the number of hours between two date-time values (an interval), and (3) How to add hours to a date-time. All of these questions have been asked and answered on StackOverflow many times before. – Basil Bourque Apr 11 '14 at 07:34
  • possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) and [this](http://stackoverflow.com/q/19658291/642706) and [this](http://stackoverflow.com/q/3581258/642706). – Basil Bourque Apr 11 '14 at 07:35

3 Answers3

1

Start by taking a look at SimpleDateFormat, which will allow you to convert the String value to a Date object.

For example...

try {
    // Note hh is Hour in am/pm (1-12), based on you example, it's not possible
    // now the day part (ie am or pm), you could supply aa as the am/pm marker
    // or use HH which is Hour in day (0-23)
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm");
    Date date = sdf.parse("2014/04/11 4:46");
    System.out.println(date);
} catch (ParseException exp) {
    exp.printStackTrace();
}

Then you can use JodaTime to calculate the difference between the two dates, see How to find difference between two Joda-Time DateTimes in minutes for an example

It should be noted that you could skip the use of SimpleDateFormat and JodaTime all the way, check out String to joda LocalDate in format of "dd-MMM-yy" for an example of converting a String to a LocalDate using JodaTime

To add time to an existing Date, you can use either Calendar or JodaTime, see how to add days to java simple date format for an example of both

I would recommend that if you are using some kind of GUI, you might consider using one of the available date pickers as it will save you a lot of hassel

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

I suggest using Java8's java.util.time package

Example:

public static void main(String[] args) {
    // example input
    String dateString1 = "2014/04/10 00:00";
    String dateString2 = "2014/04/11 23:59";

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
    LocalDateTime date1 = LocalDateTime.parse(dateString1, dtf);
    LocalDateTime date2 = LocalDateTime.parse(dateString2, dtf);


    // do your stuff with the dates...
}
ifloop
  • 8,079
  • 2
  • 26
  • 35
0

Here is my solution:

        String date1 = "2014/04/10 15:30";
    String date2 = "2014/04/11 09:00";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");

    try {
        Date parsedDate1 = sdf.parse(date1);
        Date parsedDate2 = sdf.parse(date2);
        double secs = (parsedDate2.getTime() - parsedDate1.getTime()) / 1000;
        double hours = secs / 3600;

        System.out.println(hours);

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

For adding hours to a date:

Date date = new Date(someDateObject.getTime() + 55 * 3600 * 1000);
0riginal
  • 137
  • 1
  • 11
  • What if the date itself is different ie yesterday and tomorrow? Your diff calculation would be wrong... – MadProgrammer Apr 11 '14 at 07:03
  • you can change the date as you like but if date2 is before date1 you will receive a negative value.String date1 = "2014/04/02 15:30"; String date2 = "2013/04/02 14:30"; will return -8761.0 – 0riginal Apr 11 '14 at 07:11
  • What happens when you want to know if it's been x number of days, but in hours, like 48 hours, for example... – MadProgrammer Apr 11 '14 at 07:12