Reputation: 21
I have a timestamp in cdt format as below "2023-06-05T09:00:01CDT"
, I want to convert this timestamp into MM/DD
format in Java.
I have tried below : The expected output was: 6/5 But was getting parsing error.
ZonedDateTime ztime = ZonedDateTime.parse("2023-06-05T09:00:01CDT");
Date date = Date.from(Instant.from(ztime));
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
Stack trace:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2023-06-05T09:00:01CDT' could not be parsed at index 19
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2056)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1958)
at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:600)
at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:585)
at MyClass.main(MyClass.java:9)
Upvotes: -1
Views: 167
Reputation: 27
This might be helpful.
String cdtTimestamp = "2023-06-01T15:30:00-05:00"; // Example CDT timestamp
// Parse CDT timestamp into ZonedDateTime
ZonedDateTime cdtDateTime = ZonedDateTime.parse(cdtTimestamp);
// Convert to local timezone (optional)
ZonedDateTime localDateTime = cdtDateTime.withZoneSameInstant(ZoneId.systemDefault());
// Format the date as MM/DD
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM/dd");
String formattedDate = localDateTime.format(dateFormatter);
System.out.println("Formatted date: " + formattedDate);
Upvotes: 2
Reputation: 18558
If you simply want to parse a full ZonedDateTime
String
representation that includes a time zone (three letters) instead of a standard zone id, you will have to prepare a formatter for it.
Having successfully parsed that String
, the quickest way to get a String
of the format "MM/dd"
would be to use the method format(DateTimeFormatter)
of ZonedDateTime
and define the pattern to only output month-of-year and day-of-month…
public static void main(String[] args) {
// example input
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssz");
// prepare a formatter that can actually parse the String with a time zone
ZonedDateTime ztime = ZonedDateTime.parse("2023-06-05T09:00:01CDT", dtf);
// print the full zoned date time
System.out.println(ztime);
// and print the result in a format that only considers month of year and day of month
System.out.println(ztime.format(DateTimeFormatter.ofPattern("M/d")));
}
2023-06-05T09:00:01-05:00[America/Chicago]
6/5
Upvotes: 4
Reputation: 112
Try this code snippet, if it is a parsing issue you can add a DateTimeFormatter :: documentation
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz");
ZonedDateTime time = ZonedDateTime.parse("2023-06-05T09:00:01CDT", format);
Date date = Date.from(Instant.from(time));
cal.setTime(date);
Calendar cal = Calendar.getInstance();
String date = (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
Upvotes: 1