2

I have collect the London time from Google . I want to get GMT+6 time and Date. I tried many ways but i can't reach to my destination . Can anyone please help me to solve this. I have converted all string to int .

Here is My Code :

   CurrentDate = Date[1]+"/"+Month+"/" + Date[3];
             //  String dateFormat = Date[0]
                String[] Timee = Date[4].split(":");
                int hour = Integer.parseInt(Timee[0]);

                Log.e("Timeee", hour + " -- " + Timee[1] + " --" + Timee[2]);
                int seconds = Integer.parseInt(Timee[2]);
                int minutes = Integer.parseInt(Timee[1]);
                CurrentTime = seconds + minutes + hour;

                int date = Integer.parseInt(Date[1]);
                int year = Integer.parseInt(Date[3]);

                CurrentTime2 = Date[3]+"-" + Month +"-"+"-" + Date[1] +" " + hour+":" +Timee[1]+":"+Timee[2];





                 Date date1 = null;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    date1 = sdf.parse(CurrentTime2);
                }catch (Exception e){

                }
                sdf.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
                //        System.out.println(sdf.format(date));
                Log.e("@@@Date: ",String.valueOf(sdf.format(date1)));
                 DateAndTime = String.valueOf(sdf.format(date1));
  • What is `Date[]`? – Andreas Jan 29 '18 at 04:09
  • I am getting the date from google . Date[1] Date , Date[3] = year . –  Jan 29 '18 at 04:10
  • If you want to specify the time zone (e.g. `GMT+6`), why don't you try specifying the time zone on the `GregorianCalendar` object? – Andreas Jan 29 '18 at 04:12
  • Did you mean UTC to GMT. – ArifMustafa Jan 29 '18 at 04:12
  • Yes UTC to GMT . I tried Timezone but it not working. Can u help me? –  Jan 29 '18 at 04:13
  • A `Date` object does not have and cannot have a time zone in it. Are you aware of this fact? – Ole V.V. Jan 29 '18 at 05:05
  • By London time, do you mean GMT? It’s not the same thing. Asking because UTC (nearly the same as GMT) sounds more likely. – Ole V.V. Jan 29 '18 at 05:39
  • I try sharma's Code. It getting Correct time. Byt showing wrong Date, Month, Year –  Jan 29 '18 at 05:48
  • If [the linked question](https://stackoverflow.com/questions/6567923/timezone-conversion) doesn’t help, the same question has been asked and answered numerous times. Just use your search engine to find the answer that helps you the best. – Ole V.V. Jan 29 '18 at 08:32
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [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 29 '18 at 12:13
  • Comment: your code is overly complicated. Question: what do you wnat GMT+6 for? If this is for presentation for users in some time zone, you should rather give that time zone, for example Asia/Almaty or Asia/Dhaka. – Ole V.V. Jan 29 '18 at 20:13

2 Answers2

0

follow @Affe Answer

Convert UTC to current locale time

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date myDate = simpleDateFormat.parse("your data string");

Updated

(GMT-6:00) America/Bahia_Banderas

(GMT-6:00) America/Belize
(GMT-6:00) America/Cancun
(GMT-6:00) America/Chicago
(GMT-6:00) America/Costa_Rica
(GMT-6:00) America/El_Salvador
(GMT-6:00) America/Guatemala
(GMT-6:00) America/Indiana/Knox
(GMT-6:00) America/Indiana/Tell_City
(GMT-6:00) America/Knox_IN
(GMT-6:00) America/Managua
(GMT-6:00) America/Matamoros
(GMT-6:00) America/Menominee
(GMT-6:00) America/Merida
(GMT-6:00) America/Mexico_City
(GMT-6:00) America/Monterrey
(GMT-6:00) America/North_Dakota/Beulah
(GMT-6:00) America/North_Dakota/Center
(GMT-6:00) America/North_Dakota/New_Salem
(GMT-6:00) America/Rainy_River
(GMT-6:00) America/Rankin_Inlet
(GMT-6:00) America/Regina
(GMT-6:00) America/Resolute
(GMT-6:00) America/Swift_Current
(GMT-6:00) America/Tegucigalpa
(GMT-6:00) America/Winnipeg
(GMT-6:00) CST
(GMT-6:00) CST6CDT
(GMT-6:00) Canada/Central
(GMT-6:00) Canada/East-Saskatchewan
(GMT-6:00) Canada/Saskatchewan
(GMT-6:00) Chile/EasterIsland
(GMT-6:00) Etc/GMT+6
(GMT-6:00) Mexico/General
(GMT-6:00) Pacific/Easter
(GMT-6:00) Pacific/Galapagos
(GMT-6:00) SystemV/CST6
(GMT-6:00) SystemV/CST6CDT
(GMT-6:00) US/Central
(GMT-6:00) US/Indiana-Starke

if you want any of time zone than follow this

you need to parse the date with same Dateformat

 SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String dd = "2018-01-29 12:35:00";




    Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        date = sdf.parse(dd);
    }catch (Exception e){

    }
    sdf.setTimeZone(TimeZone.getTimeZone("America/Belize"));
 //        System.out.println(sdf.format(date));
    Log.e("@@@Date: ",String.valueOf(sdf.format(date)));

OUTPUT

2018-01-29 01:05:00 (Gmt-6)

For Asia time zone (INDIA)

 SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String dd = "2018-01-29 12:35:00";




    Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        date = sdf.parse(dd);
    }catch (Exception e){

    }
    sdf.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
  //        System.out.println(sdf.format(date));
    Log.e("@@@Date: ",String.valueOf(sdf.format(date)));

OUTPUT

2018-01-29 12:35:00 GMT+05:30

Arjun saini
  • 4,223
  • 3
  • 23
  • 51
0

You can do it by adding 6 hours to GMT time.

  1. Get the London time and convert it to GMT.
  2. Now add 6 hours to that time as you want GMT +6 .

Calender calender = Calender.getInstance(); calender.setTime("Enter GMT Time Here); calender.add(Calender.Hour, 6); System.out.println("Date/Time Here " +calender.getTime());

Prashant Kumar Sharma
  • 1,120
  • 1
  • 11
  • 21