1. 程式人生 > >JAVA實現GMT轉換北京時間

JAVA實現GMT轉換北京時間

簡單粗暴上程式碼,把獲取到http的響應頭的GMT時間轉換成北京時間

		String urlStr = "http://aaa.bbb.cc/a.pdf";
        Set<String> headSet = new HashSet<>();
        headSet.add("Last-Modified");
        Map<String, Object> resultMap = HttpUtil.doGetHttpResponceHeader(urlStr, headSet);
        System.out.println("resultMap值=" + resultMap + "," + "當前類=HttpUtil.main()");
        String gmtTime = resultMap.get("Last-Modified").toString();
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
        Date date = dateFormat.parse(gmtTime);
        System.out.println("date=" + date + "," + "當前類=HttpUtil.main()");
		
		//加8個時區
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + 8);
        date = calendar.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatTime = sdf.format(date);
        System.out.println("formatTime=" + formatTime + "," + "當前類=HttpUtil.main()");