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)?

Masi Boo
  • 635
  • 1
  • 10
  • 24
  • 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. – Ole V.V. Jul 22 '22 at 04:16
  • Apparently related: [Converting specific string to Date using SimpleDateFormat](https://stackoverflow.com/questions/44509378/converting-specific-string-to-date-using-simpledateformat) (only promise me not to use `SimpleDateFormat`, the old and notoriously troublesome class). – Ole V.V. Jul 22 '22 at 04:19
  • Thanks a lot for your helpful comment. I post the working solution. – Masi Boo Jul 22 '22 at 06:19

2 Answers2

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]
Masi Boo
  • 635
  • 1
  • 10
  • 24
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);
Amol Gharpure
  • 129
  • 1
  • 7
  • 1
    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 – Masi Boo Jul 21 '22 at 21:16
  • I made a mistake in typing out the formatter. It should be YYYY not YYY. – Amol Gharpure Jul 21 '22 at 21:25
  • 1
    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) – Masi Boo Jul 21 '22 at 21:37
  • Can you paste your code here? – Amol Gharpure Jul 21 '22 at 21:57
  • Your substring operation is not the simplest way, and it precludes the validation that would be nice to have from considering the entire string. For why your code doesn’t work see for example [Can't parse String to LocalDate (Java 8)](https://stackoverflow.com/questions/31605411/cant-parse-string-to-localdate-java-8). – Ole V.V. Jul 22 '22 at 04:23
  • 1
    @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) – Masi Boo Jul 22 '22 at 05:48
  • @MasiBoo That exception is probably because a non-English locale is being used. There is another problem in the code, though, for which the link should be relevant. – Ole V.V. Jul 22 '22 at 06:44