import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
public class CalendarFormat {
public static final String STARTTIME = " 00:00:00";
public static final String ENDTIME = " 23:59:59";
public static final String TIME_ZONE_UTC = "UTC";
public static final String INPUT_DATE_FORMAT = "yyyyMMdd HH:mm:ss";
public static final String ISO_8601_FORMAT_MILLISECOND = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
public static void main(String[] args) {
CalendarFormat.getDateFormat("20210825_20210826", "CST");
}
public static String mySqlDateFormatInUtc(String date, String time, String inputTimezoneId) {
final TimeZone utcTimeZone = TimeZone.getTimeZone(CalendarFormat.TIME_ZONE_UTC);
SimpleDateFormat input = new SimpleDateFormat(INPUT_DATE_FORMAT);
input.setTimeZone(TimeZone.getTimeZone(inputTimezoneId));
String dateToBeformatted = date + " " + time;
SimpleDateFormat output = new SimpleDateFormat(CalendarFormat.ISO_8601_FORMAT_MILLISECOND);
output.setTimeZone(utcTimeZone);
String format = null;
try {
format = output.format(input.parse(dateToBeformatted));
System.out.println(format);
} catch (ParseException e) {
e.printStackTrace();
}
return format;
}
public static String[] getDateFormat(String dateRangeString, String inputTimezoneId) {
String[] dates = dateRangeString.split("_");
String from = CalendarFormat.mySqlDateFormatInUtc(dates[0], CalendarFormat.STARTTIME, inputTimezoneId.trim());
String to = CalendarFormat.mySqlDateFormatInUtc(dates[1], CalendarFormat.ENDTIME, inputTimezoneId.trim());
dates[0] = from;
dates[1] = to;
return dates;
}
}
This is my class.. here i am changing the time zone CST to UTC so its adding +5 hrs because of its adding 5hrs its changing the date 25-26 to 25-27 .. but i want the output same as 25-26 same even after the time zone conversion... please anyone help me out struck on this issue for 2 days...
for the above code .. output was like this
2021-08-25 T 05:00:00.000Z 2021-08-27 T 04:59:59.000Z
but i want the output as
2021-08-25 T 00:00:00.000Z 2021-08-26 T 23:59:59.000Z