1、获取小时

注意设置时区,calendar方法设置的时区可能会失效,一般用SimpleDateFormat

  1. public String getHour(long ts) {
  2. Date date1 = new Date(ts);
  3. Calendar calendar = Calendar.getInstance();
  4. calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
  5. calendar.setTime(date1);
  6. return String.valueOf(Calendar.HOUR_OF_DAY);
  7. }
  1. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH");
  2. simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
  3. String t = simpleDateFormat.format(ts);

org.apache.commons.lang.time.DateUtils