当前位置: 移动技术网 > IT编程>开发语言>Java > Java常用的时间工具类实例

Java常用的时间工具类实例

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java常用的时间工具类。分享给大家供大家参考,具体如下:

package org.zhy.date;
import java.text.dateformat;
import java.text.simpledateformat;
import java.util.calendar;
import java.util.date;
import java.util.gregoriancalendar;
/**
 * 时间类型工具类
 *
 * @author zhengyi
 *
 */
public class dateutils {
  // 时间格式定义
  public static final string date_pattern_yyyy_mm_dd = "yyyy-mm-dd"; // 2011-10-09
  public static final string yyyymmddhhmmss = "yyyymmddhhmmss";// 20111009100155
  public static final string yyyy_mm_ddhhmmss = "yyyy-mm-dd hh:mm:ss";// 2011-10-09
  // 10:01:55
  // 时间格式:年月日时分秒
  public static final int year = 1;// 年
  public static final int month = 2;// 月
  public static final int day = 3; // 日
  public static final int hourofday = 4;// 时
  public static final int minute = 5;// 分
  public static final int second = 6;// 秒
  /**
   * 将时间转换为字符串
   *
   * @param date
   *      :需要转换的时间
   * @param date_fomat
   *      :时间格式
   * @return string:转换后的格式
   */
  public static string datetostring(java.util.date date, string date_fomat) {
    dateformat df = new simpledateformat(date_fomat);
    return df.format(date);
  }
  /**
   * 根据年月日时分秒生成date并返回
   *
   * @param year
   *      :年
   * @param month
   *      :月
   * @param dayofmonth
   *      :日
   * @param hourofday
   *      :时
   * @param minute
   *      :分
   * @param second
   *      :秒
   * @return
   */
  public static date stringtodate(int year, int month, int dayofmonth,
      int hourofday, int minute, int second) {
    gregoriancalendar gc = new gregoriancalendar(year, month, dayofmonth,
        hourofday, minute, second);
    date dt = gc.gettime();
    return dt;
  }
  /**
   * 根据年月日生成date并返回
   *
   * @param year
   *      :年
   * @param month
   *      :月
   * @param dayofmonth
   *      :日
   * @return date:返回的date对象
   */
  public static date stringtodate(int year, int month, int dayofmonth) {
    gregoriancalendar gc = new gregoriancalendar(year, month, dayofmonth);
    date dt = gc.gettime();
    return dt;
  }
  /**
   * 是否为闰年
   *
   * @param date
   * @return
   */
  public static boolean isleapyear(date date) {
    gregoriancalendar gc = gctodate(date);
    return gc.isleapyear(findyearbydate(date, year));
  }
  /**
   * 获得日期中的年月日时分秒
   *
   * @param date
   *      :需要获取的时间
   * @param type
   *      :获取的类型,类内常量
   * @return
   */
  public static int findyearbydate(date date, int type) {
    calendar cd = calendar.getinstance();
    cd.settime(date);
    int number=0;
    switch (type) {
      case year :
        number= cd.get(calendar.year);
        break;
      case month :
        number= cd.get(calendar.month);
        break;
      case day :
        number= cd.get(calendar.day_of_month);
        break;
      case hourofday :
        number= cd.get(calendar.hour_of_day);
        break;
      case minute :
        number= cd.get(calendar.minute);
        break;
      case second :
        number= cd.get(calendar.second);
        break;
      default :
        number= 0;
    }
    return number;
  }
  /**
   * 私有函数,将date类型转换为gregoriancalendar类型以便类内使用
   *
   * @param date
   * @return
   */
  private static gregoriancalendar gctodate(date date) {
    gregoriancalendar gc = new gregoriancalendar();
    gc.settime(date);
    return gc;
  }
}

ps:这里再为大家推荐几款关于日期与天数计算的在线工具供大家使用:

在线日期/天数计算器:

在线万年历日历:

在线阴历/阳历转换工具:

unix时间戳(timestamp)转换工具:

更多关于java相关内容感兴趣的读者可查看本站专题:《java日期与时间操作技巧汇总》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网