java中將UTC時間轉成本地使用時間

/**
         * This method takes a formatted string and turns it into a calendar. This
         * approach is trimmed for performance, using high-speed substring access to
         * the string rather than dumb parsing
         *
         * @param date
         *            the String to read the date from
         * @return a calendar representing the date found in the string
         */
        private static Calendar getStringToCal(String date) {
            final String year = date.substring(0, 4);
            final String month = date.substring(5, 7);
            final String day = date.substring(8, 10);
            final String hour = date.substring(11, 13);
            final String minute = date.substring(14, 16);
            final String second = date.substring(17, 19);
            final int millisecond = Integer.valueOf(date.substring(20, 23));
            Calendar result =
                new GregorianCalendar(Integer.valueOf(year),
                    Integer.valueOf(month) - 1, Integer.valueOf(day),
                    Integer.valueOf(hour), Integer.valueOf(minute),
                    Integer.valueOf(second));
            result.set(Calendar.MILLISECOND, millisecond);
            result.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
            return result;

        }

如參數是2012-06-26T08:25:49.670,

 try {
                Calendar cal=getStringToCal(time);
                date = new Date(cal.getTimeInMillis());
              } catch (Exception e) {
                e = null;
             
              }

date爲轉換後的時間





發佈了53 篇原創文章 · 獲贊 12 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章