0

I want to get a dateTime which is some hours before the specified datetime. The datetime will be in string format and depending on configuration, need a datetime n hours before the given time( example 3 or 4 hours before the given time).

The time format of mine is 2020-10-20T13:00:00-05:00

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Joseph
  • 1,060
  • 3
  • 22
  • 53
  • You are sort of asking two questions in one: (1) How to parse your string into an `OffsetDateTime` (or other date-time type, but `OffsetDateTime` will be the correct, modern choice). (2) How to subtract a given number of hours from an `OffsetDateTime`. Both questions have very easy answers. Please search. (A Joda-Time `DateTime` will be a dated option but otherwise correct and easy too.) – Ole V.V. Oct 23 '20 at 01:54

1 Answers1

1

Using the modern date-time API:

import java.time.OffsetDateTime;

public class Main {
    public static void main(String[] args) {
        String dateTimeStr = "2020-10-20T13:00:00-05:00";
        OffsetDateTime odt = OffsetDateTime.parse(dateTimeStr);
        System.out.println("Given date time: " + odt);

        // 3-hours ago
        OffsetDateTime threeHoursAgo = odt.minusHours(3);
        System.out.println("Three hours ago: " + threeHoursAgo);
    }
}

Output:

Given date time: 2020-10-20T13:00-05:00
Three hours ago: 2020-10-20T10:00-05:00

Learn more about the modern date-time API at Trail: Date Time.

If you are doing it for your 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.

Using Joda-Time:

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        String dateTimeStr = "2020-10-20T13:00:00-05:00";
        DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ").withOffsetParsed();
        DateTime dateTime = dtf.parseDateTime(dateTimeStr);
        System.out.println("Given date time: " + dateTime);

        // 3-hours ago
        DateTime threeHoursAgo = dateTime.minusHours(3);
        System.out.println("Three hours ago: " + threeHoursAgo);
    }
}

Output:

Given date time: 2020-10-20T13:00:00.000-05:00
Three hours ago: 2020-10-20T10:00:00.000-05:00

Note: Check the following notice at the Home Page of Joda-Time

Joda-Time is the de facto standard date and time library for Java prior to Java SE 8. Users are now asked to migrate to java.time (JSR-310).

Using legacy API:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) throws ParseException {
        String dateTimeStr = "2020-10-20T13:00:00-05:00";
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT-5"));
        Date date = sdf.parse(dateTimeStr);
        System.out.println("Given date time: " + sdf.format(date));

        // 3-hours ago
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, -3);
        System.out.println("Three hours ago: " + sdf.format(calendar.getTime()));
    }
}

Output:

Given date time: 2020-10-20T13:00:00-05:00
Three hours ago: 2020-10-20T10:00:00-05:00

Recommendation: The date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. I suggest you should stop using them completely and switch to the modern date-time API.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110