Catalin Demergian
Catalin Demergian

Reputation: 97

offset Java time by a number of seconds

here is what I would like to do:

I have a date/time as a string, something like "January 2, 2010 10:25:30". The format is defined by localeLang (en, ja, unix, etc) and localeCountry (US, JP, etc). Knowing the string that represents the date and the format they are in (given by locale), I want to calculate the date/time shifted by a number of seconds.

example: date/time = January 2, 2010 10:25:30 seconds to shift = 5 result = January 2, 2010 10:25:35

Is there an easy way to do it using what Java offers ? I mean I wouldn't want to write all the logic myself, keeping in mind leap years and all those details ... :D

Upvotes: 0

Views: 3493

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 339472

java.time

The java.time framework is built into Java 8 and later. These classes supplant the old troublesome classes such as java.util.Date/.Calendar. And these classes supplant the highly successful Joda-Time library.

Get the current moment on the timeline in UTC.

Instant instant = Instant.now();

Apply a time zone.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );

Add 5 seconds. The "plus…" methods handle issues such as leap year, Daylight Saving Time, and other anomalies.

ZonedDateTime zdtLater = zdt.plusSeconds( 5 );

Use your language and country codes to determine a Locale object.

Locale locale = new Locale( "fr" , "CA" );  // French, Canada (Québec)

Generate string. Let the formatter automatically localize per the Locale, translating to a human language like French and using cultural formatting norms (order of elements, comma versus period, etc.) such as in Québec Canada.

DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL );
formatter = formatter.withLocale( locale );
String string = zdtLater.format( formatter );

Going the other direction. But try to avoid this direction. Localized strings of a date-time should be for display to the user, not for data-exchange.

ZonedDateTime zdtNew = ZonedDateTime.parse( string , formatter );

Upvotes: 1

assylias
assylias

Reputation: 328735

The easiest way is to parse the string to a LocalDateTime object:

String input = "January 2, 2010 10:25:30";
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMMM d, yyyy HH:mm:ss", Locale.ENGLISH);
LocalDateTime date = LocalDateTime.parse(input, fmt);

and then add 5 seconds:

LocalDateTime result = date.plus(5, ChronoUnit.SECONDS);

Upvotes: 2

celdridge91190
celdridge91190

Reputation: 121

You could instantiate a Calendar object and use the add function. Since you already know the format of the date you will be receiving, you can instantiate a SimpleDateFormat object and pass in your format and Locale. I have a sample below that works.

    Calendar calendar = Calendar.getInstance();
    String dateStringReceived = "January 2, 2010 10:25:30";
    System.out.println("Original Date: " + dateStringReceived);
    String dateFormat = "MMMM dd, yyyy HH:mm:ss";
    SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormat, Locale.US);
    calendar.setTime(dateFormatter.parse(dateStringReceived));
    calendar.add(Calendar.SECOND, 5);
    System.out.println(dateFormatter.format(calendar.getTime()));

Upvotes: 2

SEUH
SEUH

Reputation: 324

Solution #1

Parse the String to a date (How to parse date string to Date?), then add the 5 seconds, and make the date to string (with the same SimpleDateFormat object)

Solution #2

Split the string into all components, and add 5 to the last component (that should be the "seconds" component). If the result is greater than 60, add 1 to the "minutes" component and join the components so they match the old format

Upvotes: 0

Related Questions