Home > front end >  Java - Adding a time variable to the current time
Java - Adding a time variable to the current time

Time:09-17

I am trying to add a given "duration" to the current time. For example,

String runtime = "1h58m"

and I am trying to add this to the current time so that the output looks something like

The runtime of <movie name> is <runtime>. 
<Name> will end at <calculatedDate> 

where calculated date is the system current time, plust the given runtime.

This is what I currently have, but don't know how to add the runtime to current system time.

public void playMovie(Movie movie){
        SimpleDateFormat df = new SimpleDateFormat("MM/dd/Y hh:mmaa");
        Date date = new Date();
        System.out.println("The runtime of " name " is " runtime);
        System.out.println("" name " will end at " df.format(date));

CodePudding user response:

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Solution using java.time, the modern Date-Time API: I recommend you use java.time.Duration which was introduced with Java-8 as part of JSR-310 implementation to model ISO_8601#Duration.

Demo:

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String runtime = "1h58m";
        runtime = "PT"   runtime;
        Duration duration = Duration.parse(runtime);
        System.out.println(duration);

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime endTime = now.plus(duration);
        System.out.println(endTime);

        // Custom format
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/uuuu hh:mm a", Locale.ENGLISH);
        System.out.println(endTime.format(dtf));
    }
}

Output:

PT1H58M
2021-09-11T23:03:58.749831
09/11/2021 11:03 PM

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8 APIs available through desugaring and How to use ThreeTenABP in Android Project.

CodePudding user response:

Just use LocalDateTime API (since java 8) like that:

System.out.println("will end at "   LocalDateTime.now().plusHours(1).plusMinutes(30).plusSeconds(45));

CodePudding user response:

You can use the LocalTime class as shown below.

public void playMovie(Movie movie){

        // Finding Hours, Minutes, and Seconds from the String runtime
        int n = runtime.length();
        int hours = 0;
        int minutes = 0;
        int seconds = 0;
        int i=0;
        for(int j=1; j<n; j  ){
            if(runtime.charAt(j)=='h'){
                hours = Integer.parseInt(runtime.substring(i,j));
                i=j 1;
            }
            if(runtime.charAt(j)=='m'){
                minutes = Integer.parseInt(runtime.substring(i,j));
                i=j 1;
            }
            if(runtime.charAt(j)=='s'){
                seconds = Integer.parseInt(runtime.substring(i,j));
                i=j 1;
            }
        }

        //SimpleDateFormat df = new SimpleDateFormat("MM/dd/Y hh:mmaa");
        //Date date = new Date();
        System.out.println("The runtime of " name " is " runtime);
        //System.out.println("" name " will end at " df.format(date));
        
        // Current System Time using the LocalTime class
        LocalTime currentSystemTime = LocalTime.now();  

        // Adding the duration of the movie to the current system time
        LocalTime movieEndTime = currentSystemTime
                .plusHours(hours)
                .plusMinutes(minutes)
                .plusSeconds(seconds);

        System.out.println("" name " will end at "  movieEndTime);
}
  • Related