方法一:
SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dff.setTimeZone(TimeZone.getTimeZone("GMT+08"));
String ee = dff.format(new Date());
这个方法获取的结果是24小时制的,月份也正确
方法二:
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
String rt = sdf.format(calendar.getTime());
这个方法获取的结果是不是24小时制的,月份也正确
方法三:
public static String getLocalDatetimeString(String local) {
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone(local));
cal.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
String date = cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DAY_OF_MONTH);
String time = cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND);
return date + " " + time;
}
String tt = getLocalDatetimeString("GMT+8");
代码里也看出来了,这个在月份上加了一个1, 24小时制
网上流传的还有这个方法:
Time t=new Time("GMT+8"); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
t.setToNow(); // 取得系统时间。
int year1 = t.year;
int month1 = t.month;
int date2 = t.monthDay;
int hour = t.hour; // 0-23
int minute = t.minute;
int second = t.second;
这个月份上也是少了一个1,需要再加1,如 int month1 = t.month + 1;;
但是 hour 获取的值不对,不知为何