android gmt 시간 구하기
public static String convertCurrentTime(String regDate) {
if (StringUtils.isEmpty(regDate))
return null;
String result = regDate;
Logger.log("#12112 regDate->"+regDate);
try {
// 2017.02.03 경률 수정
String dif_time = "";
String sign_s = "";
int different_time_h = 0; // 시차 (시)
int different_time_m = 0; // 시차 (분)
// 시간차 포맷
SimpleDateFormat hour_dif_format = new SimpleDateFormat("HH:mm");
Date hour_dif = hour_dif_format.parse("00:00");
Calendar cal_hour_dif = Calendar.getInstance();
cal_hour_dif.setTime(hour_dif);
// GMT 구함
Date date = new Date();
DateFormat df = new SimpleDateFormat("z");
// +,- 구함
char sign = df.format(date).charAt(3);
// gmt 구함 GMT+1400
String gmt_hour = df.format(date).substring(4, 6);
String gmt_min = df.format(date).substring(7, 9);
int hour = Integer.parseInt(gmt_hour);
int min = Integer.parseInt(gmt_min);
//gmt포맷변경
SimpleDateFormat gmtFormat = new SimpleDateFormat("HH:mm");
Date gmtDate = gmtFormat.parse(gmt_hour + ":" + gmt_min);
Calendar cal_gmt = Calendar.getInstance();
cal_gmt.setTime(gmtDate);
Logger.log("#12112 cal_gmt->"+cal_gmt.getTime());
if (sign == '+') {
if (hour >= 9) { //+차이
cal_hour_dif.add(Calendar.HOUR, hour);
cal_hour_dif.add(Calendar.MINUTE, min);
cal_hour_dif.add(Calendar.HOUR, -9);
sign_s = "+:";
} else { //-차이
cal_hour_dif.add(Calendar.HOUR, 9);
cal_hour_dif.add(Calendar.HOUR, -hour);
cal_hour_dif.add(Calendar.MINUTE, -min);
sign_s = "-:";
}
dif_time = sign_s + hour_dif_format.format(cal_hour_dif.getTime());;
different_time_h = Integer.parseInt(dif_time.split(":")[1]);
different_time_m = Integer.parseInt(dif_time.split(":")[2]);
} else {
cal_hour_dif.add(Calendar.HOUR, 9);
cal_hour_dif.add(Calendar.HOUR, hour);
cal_hour_dif.add(Calendar.MINUTE, min);
sign_s = "-:";
dif_time = sign_s + hour_dif_format.format(cal_hour_dif.getTime());
different_time_h = Integer.parseInt(dif_time.split(":")[1]);
different_time_m = Integer.parseInt(dif_time.split(":")[2]);
}
// 시간 계산할 수 있게 포맷변경
SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date tempDate = sdFormat.parse(regDate);
Calendar cal = Calendar.getInstance();
cal.setTime(tempDate);
//
// //시간계산
if (dif_time.split(":")[0].equals("-")) {
different_time_h *= -1;
different_time_m *= -1;
}
cal.add(Calendar.HOUR, different_time_h);
cal.add(Calendar.MINUTE, different_time_m);
result = sdformat.format(cal.getTime());
} catch (Exception e) {
Logger.error("date error -> ", e);
e.printStackTrace();
}
return result;
}