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;
8
  • 4
    Does this answer your question? Java String to DateTime Commented Feb 16, 2021 at 17:36
  • Does this answer your question? java SimpleDateFormat Commented Feb 16, 2021 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. Commented Feb 16, 2021 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() Commented Feb 16, 2021 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. Commented Feb 16, 2021 at 18:44

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

14 Comments

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 '')
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 :
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.
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
You code is unreadable in the comments. You may either add it in the question or ask a new question.
|

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.