1

My frontend ReactJs code snippet:-

import React, { useState } from 'react';
import DateTimePicker from 'react-datetime-picker';
import './App.css';

function App() {

  const [value, onChange] = useState(new Date());

  console.log("onChage: "+value);

  return (
    <div className="App">
      <DateTimePicker
        onChange={onChange}
        value={value}
        format={'dd/mm/yyyy hh:mm'}
      />
    </div>
  );
}

export default App;

I can see console log as

onChage: Thu Jul 21 2022 13:11:32 GMT+0300 (Eastern European Summer Time)

I need to send this date time to Java Spingboot backend. For the backend I need to convert this date time to OffsetDateTime or LocalDateTime. How can I convert this?

Updated:

I tried and managed to convert to Data. By this:-

String dateStr = "Thu Jul 21 2022 13:11:32 GMT+0300 (Eastern European Summer Time)";
String[] tokens = dateStr.split("GMT");
String dateTimeStr = "";
if (tokens.length == 2){
    dateTimeStr = tokens[0].trim();
}
System.out.println(dateTimeStr);
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss", Locale.ENGLISH);

Date date = formatter.parse(dateTimeStr);
String formattedDateString = formatter.format(date);
System.out.println("date: "+formattedDateString);

There I lost time zone and offset. How do I keep the time zone and offset GMT+0300 (Eastern European Summer Time)?

3
  • 2
    Use DateTimeFormatter.ofPattern("EEE MMM d yyyy HH:mm:ss 'GMT'XX (zzzz)", Locale.ROOT). For validation parse both into an OffsetDateTime (which will use the offset, GMT+0300) and into a ZonedDateTime (which will use the time zone name, Eastern European Summer Time) and check that you got the same offset in both cases. Commented Jul 22, 2022 at 4:16
  • Apparently related: Converting specific string to Date using SimpleDateFormat (only promise me not to use SimpleDateFormat, the old and notoriously troublesome class). Commented Jul 22, 2022 at 4:19
  • Thanks a lot for your helpful comment. I post the working solution. Commented Jul 22, 2022 at 6:19

2 Answers 2

2

After the comment by Ole V.V. I mangaged to get both OffsetDateTime and ZonedDateTime. I am sharing the soluion. All credit goes to Ole V.V. Thanks a lot Ole V.V.

String dateStr = "Thu Jul 21 2022 13:11:32 GMT+0300 (Eastern European Summer Time)";

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("EEE MMM d yyyy HH:mm:ss 'GMT'XX (zzzz)", Locale.ROOT);
OffsetDateTime date1 = OffsetDateTime.parse(dateStr, dateTimeFormatter);
System.out.println(date1);
// print 2022-07-21T13:11:32+03:00


ZonedDateTime zdt = ZonedDateTime.parse(dateStr, dateTimeFormatter.withZone(ZoneId.systemDefault()));
System.out.println(zdt);
 // print 2022-07-21T13:11:32+03:00[Europe/Bucharest]
Sign up to request clarification or add additional context in comments.

Comments

0

Simplest way to do is just do this :-

String timeString = input.substring(4,24);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd YYYY HH:mm:ss");
System.out.println(LocalDateTime.parse(timeString, formatter);

7 Comments

I got exception: java.time.format.DateTimeParseException: Text 'Jul 21 2022 13:11:32' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MonthOfYear=7, WeekBasedYear[WeekFields[SUNDAY,1]]=2022, DayOfMonth=21},ISO resolved to 13:11:32
I made a mistake in typing out the formatter. It should be YYYY not YYY.
even after adding YYYY:- java.time.format.DateTimeParseException: Text 'Jul 21 2022 13:11:32' could not be parsed at index 0 at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
Can you paste your code here?
@AmolGharpure String input = "Thu Jul 21 2022 13:11:32 GMT+0300 (Eastern European Summer Time)"; String timeString = input.substring(4,24); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd YYYY HH:mm:ss"); System.out.println(LocalDateTime.parse(timeString, formatter)); Got exception: format.DateTimeParseException: Text 'Jul 21 2022 13:11:32' could not be parsed at index 0 at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2051)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.