0

So in the following code i am passing these input (03/11/2023 10:23 02:23,-4) in function convertToGMT(String date, String timezone)

from which i am getting output(to be stored in table) 03/12/2023 02:23

and the output from function convertToGMT(output --> 03/12/2023 02:23) i am passing into convertToClientDate(03/12/2023 02:23,-4) which is returning me output 03/11/2023 21:23

now the issue is the final output that i am getting from second function should be the same time which i am providing in first function. but its showing 1 hour less.

    public static String convertToGMT(String date, String timezone) {

    if (date == null || date.isEmpty()) {
        return date;
    }

    try {
        timezone = formatTimeZone(timezone);
         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/20yy HH:mm");
        TimeZone timeZone = TimeZone.getTimeZone("GMT" + timezone);
        TimeZone timeZoneGMT = TimeZone.getTimeZone("UTC");
        Date date2 = null;
        String format = getDateFormat();
        if (format.startsWith("dd")) {
            date = convertCNDateToDBFormat(date);
        }
        date2 = new Date(date);

        Calendar cal = Calendar.getInstance(timeZone);
        cal.set(date2.getYear(), date2.getMonth(), date2.getDate(), date2.getHours(),
        date2.getMinutes(), 0);    
        simpleDateFormat.format(cal.getTime());
        cal.setTimeZone(timeZoneGMT);
        simpleDateFormat.setTimeZone(timeZoneGMT);
        MyLog.v(">>>>>>>Final GMT " + simpleDateFormat.format(cal.getTime()));
        return simpleDateFormat.format(cal.getTime());
        } catch (Exception e) {
        e.getStackTrace();MyLog.e(">>>> error in convertToGTM " + e.getMessage());
        return "00/00/0000 00:00";
        }
    }

   ------------------------------
    public static String convertToClientDate(String date, String timezone) {
    if (date == null || date.isEmpty()) {
        return date;
       }
       try {
        timezone = formatTimeZone(timezone);
        String format = getDateFormat();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format + " HH:mm");
        TimeZone timeZone = TimeZone.getTimeZone("GMT" + timezone);
        TimeZone timeZoneGMT = TimeZone.getTimeZone("UTC");

        Date date2 = new Date(date);
        String []arr=date.split(" ");
        String dateCheck=arr[0].split("/")[1]+arr[1].split(":")[0];

        Calendar cal = Calendar.getInstance(timeZoneGMT);
        cal.set(date2.getYear(), date2.getMonth(), date2.getDate(), date2.getHours(),          
        date2.getMinutes(), 0);
        if(dateCheck.equals("1202")){
            cal.set(date2.getYear(), date2.getMonth(), date2.getDate(), 02, date2.getMinutes(), 0);
        }
        simpleDateFormat.format(cal.getTime());
        cal.setTimeZone(timeZone);
        simpleDateFormat.setTimeZone(timeZone);

        return simpleDateFormat.format(cal.getTime());
    } catch (Exception e) {
        e.getStackTrace();
        return "00/00/0000 00:00";
    }
}

    can someone help me out with the problem?
  • Does this answer your question? [Converting string to date with timezone](https://stackoverflow.com/questions/4203718/converting-string-to-date-with-timezone) – Abdul Aziz Barkat Mar 24 '23 at 08:02
  • no its not the solution, actually the issue is i am storing date in gmt format and while getting it from db, the date is coinciding with dst date and time. – abhinav Negi Mar 27 '23 at 14:15

0 Answers0