0
My Mongo Input Source data is 2020-04-14 00:00:00.0000000 (GMT-04:00), 
Data Type String
Error: Caused by: java.time.format.DateTimeParseException: Text '' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) ~[na:1.8.0_222]

I did:

Command.java:

DateTimeFormatter dateTimeformatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); 
List<RatingTiming> response = repo.findByserviceRequestedTimestampBetween(LocalDateTime.parse(query.getStartvalue(), dateTimeformatter), LocalDateTime.parse(query.getEndvalue(), dateTimeformatter));

Mogorepo.java :

public interface RatingResponseRepository extends MongoRepository<RatingTiming, String> {
    List<RatingTiming> findByserviceRequestedTimestampBetween(LocalDateTime startDate, LocalDateTime endDate);
}

Json.java :

@JsonFormat
(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss'")
@JsonDeserialize(using = DateDeserializer.class)
@JsonProperty("serviceRequestedTimestamp")
public LocalDateTime serviceRequestedTimestamp;
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Bsr
  • 11
  • 5
  • 4
    Does this answer your question? [Java String to DateTime](https://stackoverflow.com/questions/18823627/java-string-to-datetime) – Matthew Kerian Feb 16 '21 at 17:36
  • Does this answer your question? [java SimpleDateFormat](https://stackoverflow.com/questions/6406470/java-simpledateformat) – Akin Okegbile Feb 16 '21 at 17:47
  • 1
    *FYI:* `2020-04-14 00:00:00.0000000 (GMT-04:00)` != `2020-04-14 00:00:00.000Z` since they have different time zones. `00:00 GMT-4` is the same as `04:00 GMT+0` aka `04:00Z`. – Andreas Feb 16 '21 at 18:13
  • 1
    You can get value `2020-04-14T04:00:00Z` like this: `OffsetDateTime.parse("2020-04-14 00:00:00.0000000 (GMT-04:00)", DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSS (OOOO)")).toInstant()` – Andreas Feb 16 '21 at 18:16
  • The format you asked for, `2020-04-14 00:00:00.000Z`, is a bit funny in that it resembles ISO 8601 without being ISO 8601. The ISO 8601 standard requires a `T` before the time part, so `2020-04-14T00:00:00.000Z`. Consider whether to prefer this. – Ole V.V. Feb 16 '21 at 18:44
  • Welcome to Stack Overflow. Please learn that we very much prefer that you do a search before posting a question here, and if you don’t find the answer that way, in your question report what you found and spell out what you are still missing. It allows for more focused and hence better answers. – Ole V.V. Feb 16 '21 at 18:46
  • Thank you very much for quick responses. getting error : Caused by: java.time.format.DateTimeParseException: Text '2020-04-14 00:00:00.0000000 (GMT-04:00)' could not be parsed at index 10. I tried by keeping as String datatype, Code in repository.java : List findByserviceRequestedTimestampBetween(String startDate, String endDate); Code in Command.java :questedTimestampBetween(query.getStartvalue(), query.getEndvalue()); Collection_Class.java : @JsonProperty("serviceRequestedTimestamp") public String serviceRequestedTimestamp; – Bsr Feb 16 '21 at 19:45
  • contd : Calling following json script via putty unix to get output { "queryname": "querying rateresponse tables by eval dates", "field": "serviceRequestedTimestamp", "datatype": "string", "startvalue":"2020-04-14 00:00:00.0000000 (GMT-04:00)", "endvalue":"2020-04-16 00:00:00.0000000 (GMT-04:00)" } – Bsr Feb 16 '21 at 19:50

1 Answers1

1

Edit after question was edited: The exception message that you get, Text '' could not be parsed at index 0, means that you are trying to parse an empty string into a date-time, which fails since there was expected to be text in the string. How the empty string ends up there I cannot tell from your detached snippets.

java.time

IMHO you have put your question in the wrong way. It’s only in the simplest throw-away programs that you should want to convert a date and time from one string format to another. For any Spring Boot or other serious program you should not process dates and times as strings. You should parse them into proper datetime objects and only format them back when you need to give string output.

I recommend that you use java.time, the modern Java date and time API, for your date and time work. To parse your source string:

    DateTimeFormatter inputFormatter = new DateTimeFormatterBuilder()
            .append(DateTimeFormatter.ISO_LOCAL_DATE)
            .appendLiteral(' ')
            .append(DateTimeFormatter.ISO_LOCAL_TIME)
            .appendPattern(" (OOOO)")
            .toFormatter();
    
    String inputSource = "2020-04-14 00:00:00.0000000 (GMT-04:00)";
    
    OffsetDateTime dateTime = OffsetDateTime.parse(inputSource, inputFormatter);
    System.out.println(dateTime);

Output so far:

2020-04-14T00:00-04:00

It’s a bit longish. We could have created a DateTimeFormatter directly from a format pattern string. The advantages of using the builder include:

  • We are reusing two built-in formatters rather than building everything from scratch.
  • ISO_LOCAL_TIME accepts a variable number of decimals on the seconds, so we are getting a more flexible formatter.

To format the date and time for output I am assuming (1) that you really wanted ISO 8601 format output since your example string resembles well, and (2) that you wanted the same point in time.

    String formattedDateTime = dateTime.format(DateTimeFormatter.ISO_INSTANT);
    System.out.println(formattedDateTime);

2020-04-14T04:00:00Z

The time has been changed to 04:00 UTC, the trailing Z that you asked for meaning UTC. This is correct: when it’s 4 AM UTC, it’s only 00:00 (midnight) in a time zone at offset GMT-04:00 where the source string came from. You asked for three decimals on the seconds, but in ISO 8601 the decimals are optional when they are zero, so here they are not printed. If you need the string for data exchange with a system that requires ISO 8601, you’re set.

If you wanted the same wall-clock time, 00:00, even though it’s a different time zone, you can have that too:

    String formattedDateTime = dateTime.withOffsetSameLocal(ZoneOffset.UTC)
            .format(DateTimeFormatter.ISO_INSTANT);

2020-04-14T00:00:00Z

If you were serious about wanting the exact format that you mentioned, even though it disagrees with ISO 8601, use a home-made formatter:

    DateTimeFormatter targetFormatter
            = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSX");
    String formattedDateTime = dateTime.withOffsetSameLocal(ZoneOffset.UTC)
            .format(targetFormatter);

2020-04-14 00:00:00.000Z

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Thank you for you help and time. One of the above option is what I want but I am still getting the same error. I think issue is where I am passing input value. So my source date is STRING : 2020-04-14 09:10:57.0851800 (GMT-04:00) and expected datetime that will go with teradata driver schedule : select ((VLUTN_START_DTM (format 'yyyy-mm-dd')) (varchar(10))) || 'T' || ((VLUTN_START_DTM (format 'hh:mi:ss.s(3)')) (varchar(12))) || 'Z' (title ''), ((VLUTN_END_DTM (format 'yyyy-mm-dd')) (varchar(10))) || 'T' || ((VLUTN_END_DTM (format 'hh:mi:ss.s(3)')) (varchar(12))) || 'Z' (title '') (title '') – Bsr Feb 17 '21 at 16:28
  • cntd: I tried changing teradata dateformat to GMT-04:00 format but not working. For now, I was calling json script in unix: "queryname": "querying mongo source", "field": "serviceRequestedTimestamp", "datatype": "string", "startvalue":"2020-04-14 00:00:00.0000000 (GMT-04:00)", "endvalue":"2020-04-15 00:00:00.0000000 (GMT-04:00)". Getting error : – Bsr Feb 17 '21 at 16:32
  • Not knowing Teradata I don’t think I can add much. Beware of hardcofing `Z` as a literal, though, it’s an offset of 0 from UTC, so if your date and time are not in UTC, you will get wring results. – Ole V.V. Feb 17 '21 at 16:32
  • error: Caused by: java.time.format.DateTimeParseException: Text '2020-04-14 00:00:00.00 00000 (GMT-04:00)' could not be parsed at index 10at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.j at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) java.time.LocalDateTime.parse(LocalDateTime.java:492)com.lmig.grm.us.dae.command.RateResponseCommand.run(RateResponseComma – Bsr Feb 17 '21 at 16:33
  • My Java code as per suggestion above : mongo repository.java : public interface RatingResponseRepository extends MongoRepository { List findByserviceRequestedTimestampBetween(OffsetDateTime startDate, OffsetDateTime endDate); } Collection.java: @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS'Z'") @JsonDeserialize(using = LocalDateTimeDeserializer.class) @JsonProperty("serviceRequestedTimestamp") public LocalDateTime serviceRequestedTimestamp; – Bsr Feb 17 '21 at 16:38
  • App.java : public class App { public static void main(String[] args) { TimeZone.setDefault(TimeZone.getTimeZone("UTC")); SpringApplication.run(App.class, args); Command.java: DateTimeFormatter dateTimeformatter = new DateTimeFormatterBuilder() .append(DateTimeFormatter.ISO_LOCAL_DATE).appendLiteral(''.append(DateTimeFormatter.ISO_LOCAL_TIME).appendPattern(" (OOOO)").toFormatter(); List response = repo.findByserviceRequestedTimestampBetween(OffsetDateTime.parse(query.getStartvalue(),dateTimeformatter),OffsetDateTime.parse(query.getEndvalue(),dateTimeformatter)); – Bsr Feb 17 '21 at 16:40
  • 1
    You code is unreadable in the comments. You may either add it in the question or ask a new question. – Ole V.V. Feb 17 '21 at 16:41
  • okay, I will create a new question. I appreciate your help. Thank you! – Bsr Feb 17 '21 at 16:56
  • I have updated main question. Could someone please guide me. Thank you in advance! – Bsr Feb 18 '21 at 23:56
  • There isn’t much I can tell you, @Bsr, I have added a short paragraph at the start of the answer. – Ole V.V. Feb 19 '21 at 05:43
  • @Ole.V.V, thank you for your guidance. I was trying what you have given above, regarding : String formattedDateTime = dateTime.format(DateTimeFormatter.ISO_INSTANT); System.out.println(formattedDateTime); It will give output 2020-04-14T04:00:00Z which is what i am looking to convert string 2020-04-14 00:00:00.0000000 (GMT-04:00) source. Are we supposed to use dateTime.format(DateTimeFormatter.ISO_INSTANT); with pattern append OOO pattern mentioned above. – Bsr Feb 19 '21 at 18:45
  • I’m afraid I didn’t get the question? Aren’t you saying that my code is giving you what you want? – Ole V.V. Feb 19 '21 at 18:50
  • I mean in an example that you have posted above : 2020-04-14T00:00:00Z is what I want on my output. You have used String formattedDateTime = dateTime.format(DateTimeFormatter.ISO_INSTANT); Are you using this with something before this to convert string (2020-04-14 00:00:00.0000000 (GMT-04:00))to datetime? – Bsr Feb 19 '21 at 18:54
  • Yes, it’s in the first code snippet of the answer. You may simply concatenate the two snippets. – Ole V.V. Feb 19 '21 at 20:49