Reputation: 3
My input: 2021-01-07T18:54:00.000 - UTC(TZ)
My expected output:
GMT +5:30 - 2021-01-08T00:24:00.000
GMT -12:00 - 2021-01-07T06:54:00.000
Calendar calendar = Calendar.getInstance();
// This line is creating the calendar instance for the current day, I need to create calendar instance for the string which i receive from API
calendar.setTime(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
//Here you set to your timezone
sdf.setTimeZone(TimeZone.getTimeZone("GMT+05:30"));
String temp1 = sdf.format(calendar.getTime());
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH);
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd-MM-yyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(temp1, inputFormatter);
String formattedDate = outputFormatter.format(date);
System.out.println(formattedDate);
Upvotes: 0
Views: 63
Reputation: 86272
I’ll take the big step back and suggest other and probably better ways to do your things. I don’t know your big picture, so it may be that not all of my general suggestions apply to your situation and requirements. But I consider it likely that they do.
Instant
objects. When you take string input, parse it into an Instant
. Only when you need to give string output, convert your Instant
to a ZonedDateTime
in the appropriate time zone and format it into the required string.Z
, for example, 2021-01-07T18:54:00.000Z
. Other offsets are typically given in +HH:MM
format, for example 2021-01-08T00:24:00.000+05:30
or 2021-01-07T06:54:00.000-12:00
.Asia/Kolkata
or Asia/Colombo
.SimpleDateFormat
, Calendar
and TimeZone
. They were poorly designed and are long outdated. You were already on the way with DateTimeFormatter
and LocalDate
from java.time. Just go all-in.I guessed that it was not user input because while ISO 8601 is readable by humans, for user input you would probably have got a still more user-friendly, localized format. So I am assuming that you get an ISO 8601 string in UTC from some other system. Convert to Instant
like this:
String sDate1="2021-01-05T00:00:00Z"; // The trailing Z means UTC
Instant inst = Instant.parse(sDate1);
System.out.println(inst);
Output so far is:
2021-01-05T00:00:00Z
Notice that we didn’t need to specify any formatter. Instant
parses ISO 8601 format in UTC with the trailing Z
just like that. An Instant
is a point in time independent of timezone.
On the same note, I am assuming that you need to give string output in some time zone to another system (maybe some front-end).
ZoneId zone = ZoneId.of("Asia/Colombo");
String output = inst.atZone(zone)
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
System.out.println(output);
2021-01-05T05:30:00+05:30
Again we did not need to construct any formatter ourselves. I just used one that was built in.
Upvotes: 1
Reputation: 3
String sDate1="2021-01-05T00:00:00";
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
//UTC
isoFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = isoFormat.parse(sDate1);
System.out.println(isoFormat.format(date));
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//Here you set to your timezone
isoFormat.setTimeZone(TimeZone.getTimeZone("GMT+05:30"));
System.out.println(isoFormat.format(calendar.getTime()));
//Here you set to your timezone
isoFormat.setTimeZone(TimeZone.getTimeZone("GMT-12:00"));
System.out.println(isoFormat.format(calendar.getTime()));
Upvotes: 0