javajava-timejava-calendar

Java how to remove nth minute from given time string without considering time zone


I tried to remove minute from given time, but some how it is converting time to my local time zone

String timeStamp="20180623 05:58:15" ;   
dateFormat inputFormatter = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
                Date date = inputFormatter.parse(timeStamp); 
                date.setMinutes(-2);
                logger.info("Before converting : "+date);
                DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); 

Here it is converting to my local time and subtracting 2 minutes from given time, but I don`t want to check the time zone here instead, what ever time I give it should just subtract 2 minutes.


Solution

  • Start with understanding into how Date works. When you do...

    logger.info("Before converting : "+date);
    

    The Date class uses it's toString method to format the the date/time information represented by the Date class into a human readable format. It doesn't "convert" the date/time value in anyway

    So taking your code from above (and reworking it so it works), it outputs...

    Before converting : Sat Jun 23 04:58:15 AEST 2018
    20180623 04:58:15
    

    on my machine - why are the values the same? Because the input doesn't have any time zone information, so the time is likely been treated as been in the machines local timezone (and the value is simply been formatted for output).

    Date is just a container for the number of milliseconds since the Unix Epoch, it's format agnostic - meaning it carries not formatting information.

    Date is also effectively deprecated - not to mention that setDate is also very much deprecated

    A better (starting point) overall is to make use the newer date/time API introduced in Java 8 (and which has back port support for earlier versions of the API)

        String timeStamp = "20180623 05:58:15";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss", Locale.ENGLISH);
        LocalDateTime ldt = LocalDateTime.parse(timeStamp, formatter);
        ldt = ldt.minusMinutes(2);
        System.out.println(ldt);
        System.out.println(ldt.format(formatter));
    

    This will output...

    2018-06-23T05:56:15
    20180623 05:56:15
    

    The input and the output are still consider as been in the machines local time zone.

    but I don`t want to check the time zone here instead, what ever time I give it should just subtract 2 minutes

    Just remember, the API still needs to have some concept of time zone, weather it's the local time zone or UTC/GMT, but since your input doesn't provide any kind of information, you need to make a choice over "how" best to handle that issue. The example above just "assumes" local time, but you could use ZonedDateTime and convert it to "common" time zone from which your operations are executed or, better yet, make all your strings carry time zone information

    Oh, and for the love of my sanity, stop managing date/time values in String format - get them into an appropriate container as soon as possible and manage them from there - I've spent a week wrangling inappropriately formatted date strings and I'm not happy Jan, not happy