I have a birthday (Date
object) that I would like to convert to UTC time.
I am able to use formatting to convert my locale time to UTC time by SimpleDatePattern
. However when I format it, it converts the Date
object to String
. I tried parsing it (which returns a Date
object) but converts it to local time system again. Here is my code below. Any help on converting the birthday field to UTC time while remaining a Date
object and not a string?
public Date getBirthDay() {
if (birthDay == null) {
return null;
}
DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String formattedBirthDate = utcFormat.format(birthDay);
try {
System.out.println("I am here within try block" + utcFormat.parse(formattedBirthDate));
birthDay = utcFormat.parse(formattedBirthDate);
} catch (ParseException e) {
e.printStackTrace();
}
return (Date) birthDay.clone();
}