I have a value like the following Mon Jun 18 11:32:05 BST 2012
and I want to convert this to 18 June 2012
How to convert this?
Below is what i have tried..
public String getRequiredDate(String sendInputDate) {
Optional<OffsetDateTime> date = dateParsing(sendInputDate);
if(date) {
String REQUIRED_PATTERN="dd MMMM yyyy";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(REQUIRED_PATTERN);
return date.get().format(formatter);
}
}
import java.time.OffsetDateTime;
hence returning in same
public static Optional<OffsetDateTime> dateParsing(String date) {
String PATTERN = "EEE MMM dd HH:mm:ss zzz yyyy";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN);
return Optional.of(OffsetDateTime.parse(date, formatter));
}
But its always returning me " " (blank)
CodePudding user response:
It doesn't "return blank". You presumably have some exception handling somewhere which is swallowing the actual issue and replacing that with a blank string.
The actual issue is this exception:
java.time.format.DateTimeParseException: Text 'Mon Jun 18 11:32:05 BST 2012' could not be parsed:
Unable to obtain OffsetDateTime from TemporalAccessor
i.e. your string does not contain an offset, but you are trying to parse it into something which requires one. It has a zone.
You could parse it into a ZonedDateTime and then convert it, for example.
OffsetDateTime parsed = ZonedDateTime.parse(date, formatter).toOffsetDateTime();