How to Convert a 2021-09-29T17:04:31.0000 05:30
to 2021-09-29 17:04:31.0000000 Asia/Calcutta
In Java?
and additional question is here-
I tried many ways to get the timezone(Asia/Calcutta)
from offset( 05:30)
but unable to get.
Can someone please help?
CodePudding user response:
Since you cannot derive a zone from an offset (reliably), you may want to use a workaround / different approach.
Here's an example that parses the input String
to an OffsetDateTime
using a suitable DateTimeFormatter
and then creates the desired ZoneId
which it applies to the OffsetDateTime
in order to create a ZonedDateTime
.
public static void main(String[] args) {
// your example input
String offsetTime = "2021-09-29T17:04:31.0000 05:30";
// define a formatter that parses a String of this format
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSS xxx");
// then parse it to an object that knows date, time and offset
OffsetDateTime odt = OffsetDateTime.parse(offsetTime, dtf);
// print the result
System.out.println(odt);
// now define the desired zone
ZoneId asiaCalcutta = ZoneId.of("Asia/Calcutta");
// and use the offset-aware object to apply the zone
ZonedDateTime zdt = odt.atZoneSameInstant(asiaCalcutta);
// print it
System.out.println(zdt);
}
This outputs
2021-09-29T17:04:31 05:30
2021-09-29T17:04:31 05:30[Asia/Calcutta]
Please note: This will only keep the same offset if the offset and the zone actually match. I don't know your concrete requirements, so only you can know if this approach is appropriate in your situation or not.
If you want to find out time zones that currently use a specific offset and then make your code decide which is the correct one, you may want to have a look at this question and its answers.
CodePudding user response:
I tried to write this code to cover three possibilities String parameter , or LocalDateTime parameter and ZonedDateTime Parameter :
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class SpecialFormater {
// parameter : ZonedDateTime
public static String specialFormaterZonedDateTime(ZonedDateTime zdt) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSS");
ZoneId zid = zdt.getZone();
System.out.println("Your initial format =" zdt);
String result = "Your expected format =" zdt.format(formatter) " " zid;
return result;
}
// parameter : LocalDateTime
public static String specialFormaterLocalDateTime(LocalDateTime dt) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSS");
ZoneId zid = ZoneId.of("Asia/Calcutta");
System.out.println("Your initial format =" dt);
ZonedDateTime zdt = ZonedDateTime.of(dt, zid);
System.out.println("Your zoned dateTime default format =" zdt);
String result = "Your expected format =" dt.format(formatter) " " zid;
return result;
}
// Parameter : String
public static String specialFormaterString(String localDateTime) {
System.out.println("String before formatting =" localDateTime);
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSS xxx";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime dt = LocalDateTime.parse(localDateTime, formatter);
ZoneId zid = ZoneId.of("Asia/Calcutta");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSS");
String result = "String after formatting =" dt.format(formatter2) " " zid;
return result;
}
public static void main(String[] args) {
System.out.println("specialFormaterLocalDateTime");
System.out.println(specialFormaterLocalDateTime(LocalDateTime.now()));
System.out.println("***********");
System.out.println();
System.out.println("specialFormaterString");
System.out.println(specialFormaterString("2021-09-29T17:04:31.0000 05:30"));
System.out.println("**********");
System.out.println();
System.out.println("specialFormaterZonedDateTime");
System.out.println(specialFormaterZonedDateTime(ZonedDateTime.now()));
}
}
Output :
specialFormaterLocalDateTime
Your initial format =2021-10-06T12:35:45.029
Your zoned dateTime default format =2021-10-06T12:35:45.029 05:30[Asia/Calcutta]
Your expected format =2021-10-06 12:35:45.0290000 Asia/Calcutta
***********
specialFormaterString
String before formatting =2021-09-29T17:04:31.0000 05:30
String after formatting =2021-09-29 17:04:31.0000000 Asia/Calcutta
**********
specialFormaterZonedDateTime
Your initial format =2021-10-06T12:35:45.064 02:00[Europe/Paris]
Your expected format =2021-10-06 12:35:45.0640000 Europe/Paris