Android UTCとLocal時間が逆転
6191 ワード
public static String getUTCFromLocalTime_(int hour, int minute, int second) {
StringBuffer UTCTimeBuffer = new StringBuffer();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
// 1、 :
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), hour, minute, second);
// 2、 :
int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
// 3、 :
int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
Log.d("xxx", "getUTCFromLocalTime ZONE_OFFSET : " + zoneOffset
+ " ,DST_OFFSET: " + dstOffset
+ ",TimeZone:" + TimeZone.getDefault()
+ ",Local:" + Locale.getDefault());
// 4、 , UTC :
cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
int yearCal = cal.get(Calendar.YEAR);
int monthCal = cal.get(Calendar.MONTH);
int dayCal = cal.get(Calendar.DAY_OF_MONTH);
int hourCal = cal.get(Calendar.HOUR_OF_DAY);
int minuteCal = cal.get(Calendar.MINUTE);
int secondCal = cal.get(Calendar.SECOND);
UTCTimeBuffer
.append(yearCal)
.append("-")
.append(monthCal)
.append("-")
.append(dayCal)
.append(" ")
.append(hourCal < 10 ? ("0" + hourCal) : hourCal)
.append(":")
.append(minuteCal < 10 ? ("0" + minuteCal) : minuteCal)
.append(":")
.append(secondCal);
try {
format.parse(UTCTimeBuffer.toString());
Log.i("", "UTC time-->" + UTCTimeBuffer.toString());
return UTCTimeBuffer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getLocalTimeFromUTC_(int hour, int minute, int second) {
StringBuffer UTCTimeBuffer = new StringBuffer();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
// 1、 :
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), hour, minute, second);
// 2、 :
int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
// 3、 :
int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
Log.d("xxx", "getUTCFromLocalTime ZONE_OFFSET : " + zoneOffset
+ " ,DST_OFFSET: " + dstOffset
+ ",TimeZone:" + TimeZone.getDefault()
+ ",Local:" + Locale.getDefault());
// 4、 UTC , :
cal.add(java.util.Calendar.MILLISECOND, (zoneOffset + dstOffset));
// System.out.println("p: " + cal.getTimeInMillis());
// System.out.println("UTC:" + new Date(cal.getTimeInMillis()));
int yearCal = cal.get(Calendar.YEAR);
int monthCal = cal.get(Calendar.MONTH);
int dayCal = cal.get(Calendar.DAY_OF_MONTH);
int hourCal = cal.get(Calendar.HOUR_OF_DAY);
int minuteCal = cal.get(Calendar.MINUTE);
int secondCal = cal.get(Calendar.SECOND);
UTCTimeBuffer
.append(yearCal)
.append("-")
.append(monthCal)
.append("-")
.append(dayCal)
.append(" ")
.append(hourCal < 10 ? ("0" + hourCal) : hourCal)
.append(":")
.append(minuteCal < 10 ? ("0" + minuteCal) : minuteCal)
.append(":")
.append(secondCal);
try {
format.parse(UTCTimeBuffer.toString());
Log.i("", "localTime:" + UTCTimeBuffer.toString());
return UTCTimeBuffer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
以下の転載:https://blog.csdn.net/Burn_yourself/article/details/71195241
/**
* ---> UTC
* @return
*/
public static String Local2UTC(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("gmt"));
String gmtTime = sdf.format(new Date());
return gmtTime;
}
/**
* ---> UTC
* @return
*/
public static String Local2UTC(String localTime){
SimpleDateFormat localFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//
localFormater.setTimeZone(TimeZone.getDefault());
Date gpsLocalDate = null;
try {
gpsLocalDate = localFormater.parse(localTime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat utcFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//UTC
utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));
String UTCTime = utcFormater.format(gpsLocalDate.getTime());
return UTCTime;
}
/**
* UTC --->
* @param utcTime UTC
* @return
*/
public static String utc2Local(String utcTime) {
SimpleDateFormat utcFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//UTC
utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));
Date gpsUTCDate = null;
try {
gpsUTCDate = utcFormater.parse(utcTime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat localFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//
localFormater.setTimeZone(TimeZone.getDefault());
String localTime = localFormater.format(gpsUTCDate.getTime());
return localTime;
}