1

I want to convert a PST Time which is in format of MM/dd/yyyy HH:mm to MST Time.

Code which i tried is below. Currently i am in IST Time Zone and using JDK1.7.

The below code produces the Output This is the Code Test : 08/31/2015 07:20 MST

I have a doubt whether it is really a MST or not! because the MST Time here shows me as 20:50 so i taught, it should either display as 8:50 considering the am/pm (12hr) clock.

This is what confused me-I want to know what went wrong?

   /**
     * 
     * Read a Date in the Format of MM/dd/yyyy HH:mm which is a (PST) Time Zone.
     * Convert it to MST Time Zone and Save.
     * 
     * Step 1 : Set Timezone as PST to Date in format of MM/dd/yyyy HH:mm,
     * Step 2 : Convert PST to MST Time,
     * Step 3 : Convert MST Time to Date in format of MM/dd/yyyy HH:mm.
     * 
     * Eg:  PST Time Now -> 08/31/2015 19:50;
     *      MST Time Now -> 08/31/2015 20:50;
     * 
     *
     */

 package dates;

        import java.text.DateFormat;
        import java.text.ParseException;
        import java.text.SimpleDateFormat;
        import java.util.Calendar;
        import java.util.Date;
        import java.util.Locale;
        import java.util.TimeZone;


        public class DateUtilities {

            public static void main(String[] args) {
                String dateInString = "08/31/2015 19:50";
                System.out.println("This is the Code Test"+MstTimeNow(step1(StringToDate(dateInString))));
                // step2(step1(StringToDate(dateInString)));
            }


            // String To Date
            public static Date StringToDate(String dateInString) {
                Date date = null;
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
                    date = sdf.parse(dateInString);
                    // System.out.println("String To Date "+date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return date;
            }


            // To Set TimeZone as PST
            public static Calendar step1(Date date) {
                Calendar cal = Calendar.getInstance();
                TimeZone tz = TimeZone.getTimeZone("PST");
                cal.setTimeZone(tz);
                cal.setTime(date);
                // System.out.println("Get Calendar PST Time: "+cal.getTime() +" TimeZone "+cal.getTimeZone());
                return cal;
            }

            // Calendar To MST Time
            public static String MstTimeNow(Calendar cal) {
                DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm z");
                formatter.setCalendar(cal);
                formatter.setTimeZone(TimeZone.getTimeZone("MST"));
                String mstTimeString = formatter.format(cal.getTime());
                // System.out.println("MST time Now is : "+mstTimeString);
                return mstTimeString;
            }
    }

Solution given by assylias produces an output below which is what expected!

Input String : 08/31/2015 19:50
Local Date Time: 2015-08-31T19:50
Zoned Date Time PST: 2015-08-31T19:50-07:00[America/Los_Angeles]
Zoned Date Time MST: 2015-08-31T20:50-06:00[America/Denver]
Community
  • 1
  • 1
09Q71AO534
  • 4,300
  • 13
  • 44
  • 68
  • I am expecting that my code above should return me time as described in the `Eg` – 09Q71AO534 Aug 31 '15 at 15:09
  • What is your problem? How is your Question not already addressed by the hundreds of similar Questions and Answers already posted? – Basil Bourque Aug 31 '15 at 15:10
  • 2
    Tip: Never use the 3-4 letter time zone codes. Use [proper time zone names](https://en.m.wikipedia.org/wiki/List_of_tz_database_time_zones). – Basil Bourque Aug 31 '15 at 15:12
  • @BasilBourque I have given a edit on brief of What my Question is? and please advice which TimeZone in the above Link means a PST / MST i am very bad at zones. – 09Q71AO534 Aug 31 '15 at 15:21
  • I will have a try on this `America/Los_Angeles - Pacific Time; America/Denver - Mountain Time;` – 09Q71AO534 Aug 31 '15 at 15:25
  • You posted way too much code. Read [SSCCE](http://www.sscce.org). Trim to the minimum needed to show your issue. Clearly document your inputs, outputs, and expected results. And please search StackOverflow before posting. Any basic date-time question has almost certainly been handled already. – Basil Bourque Aug 31 '15 at 15:33
  • Also duplicate of [this](http://stackoverflow.com/q/13197956/642706) and [this](http://stackoverflow.com/q/23672326/642706). – Basil Bourque Aug 31 '15 at 16:01
  • @BasilBourque Sorry! to dis-agree your comments on duplicates! As, my problem is all about the `Output` which is produced by the above code. If you observe i have given a `Eg` of Expected Output. – 09Q71AO534 Sep 01 '15 at 11:11
  • yes! I did a search in SO as well and i found huge and huge questions! I May give the question a `same title` but my context of question is different – 09Q71AO534 Sep 01 '15 at 11:12

2 Answers2

0

Your code could be a lot simpler with the new Java time API if you can use it:

String input = "08/31/2015 19:50";
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm")

LocalDateTime ldt = LocalDateTime.parse(input, fmt);
ZonedDateTime pst = ZonedDateTime.of(ldt, ZoneId.of("America/Los_Angeles"));
ZonedDateTime mst = pst.withZoneSameInstant(ZoneId.of("America/Denver"));
assylias
  • 321,522
  • 82
  • 660
  • 783
0

I tried the following code in Java1.7, the below code is a part of conversion from PST to MST only. :P

public static Date convertPstToMst(String dateInString){
          Date mstDate = null;

          // Set Default TimeZone as PST    
          SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
          sdf.setTimeZone(TimeZone.getTimeZone(PST_TIMEZONE));

          try { 
                Calendar pstCal = Calendar.getInstance(TimeZone.getTimeZone(PST_TIMEZONE));
                pstCal.setTime(sdf.parse(dateInString));

                    String pstTime = ((pstCal.get(Calendar.MONTH)+1)+"/"+pstCal.get(Calendar.DATE)+"/"+pstCal.get(Calendar.YEAR)+" "+pstCal.get(Calendar.HOUR_OF_DAY)+":"+pstCal.get(Calendar.MINUTE));
                    System.out.println(pstTime);

                    pstCal.setTimeZone(TimeZone.getTimeZone(MST_TIMEZONE));
                    String mstTime = ((pstCal.get(Calendar.MONTH)+1)+"/"+pstCal.get(Calendar.DATE)+"/"+pstCal.get(Calendar.YEAR)+" "+pstCal.get(Calendar.HOUR_OF_DAY)+":"+pstCal.get(Calendar.MINUTE));
                    System.out.println(mstTime);
                    // Converting String to Date
                    mstDate = convertStringToDate(mstTime);

            } catch (ParseException e) {
                e.printStackTrace();
            }
          return mstDate;
      }
09Q71AO534
  • 4,300
  • 13
  • 44
  • 68