Date selected through datepicker shwing one less day when rendered in htl
I have added a datepicker field of datetime type in page properties and trying to retrieve that datetime in component through servlet/sling model. I'm using
formatDate(pageProperties.get("newsPublishedDate", Date.class)) to get the date.
And here is the formatDate function:
private String formatDate(Date date) {
String daySuffix = "th";
int dayOfMonth = Integer.parseInt(new SimpleDateFormat("d").format(date));
if (!(dayOfMonth >= 11 && dayOfMonth <= 13)) {
if (dayOfMonth % 10 == 1) {
daySuffix = "st";
} else if (dayOfMonth % 10 == 2) {
daySuffix = "nd";
} else if (dayOfMonth % 10 == 3) {
daySuffix = "rd";
}
}
SimpleDateFormat sdf = new SimpleDateFormat("d'"+daySuffix+"' MMM yyyy");
return sdf.format(date);
}
private String formatDate(Date date) {
String daySuffix = "th";
int dayOfMonth = Integer.parseInt(new SimpleDateFormat("d").format(date));
if (!(dayOfMonth >= 11 && dayOfMonth <= 13)) {
if (dayOfMonth % 10 == 1) {
daySuffix = "st";
} else if (dayOfMonth % 10 == 2) {
daySuffix = "nd";
} else if (dayOfMonth % 10 == 3) {
daySuffix = "rd";
}
}
SimpleDateFormat sdf = new SimpleDateFormat("d'"+daySuffix+"' MMM yyyy");
return sdf.format(date);
}
This works fine on local but when deployed on cloud server, it starts showing the date in UTC time zone ,i.e., one less day. How can I get the exact date which I'm entering into the dtepicker field. Attaching images for the same: 


