0

How to convert newyork timezone time to "Asia/Kolkata" date and time?

my Input String:

11/28/2012 8:59am

My code:

String dtStart = "11/28/2012 8:59am";  
        SimpleDateFormat  format = new SimpleDateFormat("mm/dd/yyyy hh:mma");  
        format.setTimeZone(TimeZone.getTimeZone("America/New_York"));
        try {  
            Date date = format.parse(dtStart); 
            Log.i("clock", date.toString());
            System.out.println(date);


            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(date.getTime());
            cal.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
            int chour = cal.get(Calendar.HOUR_OF_DAY);
            int cminute = cal.get(Calendar.MINUTE);
            int dd = cal.get(Calendar.DAY_OF_MONTH);
            int mm =cal.get(Calendar.MONTH);
            int yy =cal.get(Calendar.YEAR);

            String mode="AM";

            if(chour>12)
            {
                chour=chour-12;
                mode="PM";
            }

            String mytime=Integer.toString(chour)+":"+Integer.toString(cminute)+" "+mode;
            String mydate=Integer.toString(dd)+"/"+Integer.toString(mm)+"/"+Integer.toString(yy);

            Log.i("clock", mytime);
            Log.i("clock", mydate);

        } catch (ParseException e) {  
            e.printStackTrace();  
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

My output Log:

Sat Jan 28 19:29:00 GMT+05:30 2012
7:29 PM
28/0/2012

Here my time is correct,but date is wrong. I expect 28/11/2012.I am not able trace where i did wrong?

Mister Smith
  • 27,417
  • 21
  • 110
  • 193
Ramprasad
  • 7,981
  • 20
  • 74
  • 135
  • Shouldn't the Asia time be a day or so in the future from New York? – kabuto178 Nov 28 '12 at 14:51
  • For any newcomer to this question: consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jan 08 '21 at 18:22

2 Answers2

4

the issue is in format you entered: "mm/dd/yyyy hh:mma". I believe you wanted to do this: "MM/dd/yyyy hh:mma"

ibecar
  • 415
  • 3
  • 10
  • 1
    I dont know why there was ** added.. the point is, you have used lowercase "m" which relates to minutes instead of uppercase M which relates to months – ibecar Nov 28 '12 at 18:32
0
SimpleDateFormat formatter = new SimpleDateFormat("MM/DD/YYYY hh:mm:ss a");
String dtStart = "11/28/2012 08:59 am";
Date dateObj = formatter.parse(dtStart); 
cal.setTime(dateObj);
fromTZObj = TimeZone.getTimeZone("America/New_York");
toTZObj = TimeZone.getTimeZone("Asia/Kolkata");
Calendar fromCal = new GregorianCalendar(fromTZObj);
fromCal.set(cal.get(1), cal.get(2), cal.get(5), cal.get(11), cal.get(12), 
cal.get(13));
Calendar toCal = new GregorianCalendar(toTZObj);
toCal.setTimeInMillis(fromCal.getTimeInMillis());
Date dd = toCal.getTime();
formatter.setTimeZone(toTZObj);
String formattedDate = format.format(dd);
System.out.println(formattedDate);
Zain
  • 37,492
  • 7
  • 60
  • 84
  • While it was reasonable to use `SimpleDateFormat`, `Date`, `TimeZone` and `Calendar` when this question was asked in 2012, it has long not been. Those classes are poorly designed and long outdated. Instead use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 08 '21 at 18:19