Home  >  Article  >  Java  >  Briefly describe the commonly used time tool classes in Java

Briefly describe the commonly used time tool classes in Java

零下一度
零下一度Original
2017-06-17 12:00:171228browse

This article mainly introduces the commonly used time tool classes in Java, and analyzes the common conversion, judgment, and output related operation skills of Java date and time in the form of specific examples. Friends in need can refer to the examples in this article

Describes commonly used time tool classes in Java. Share it with everyone for your reference, the details are as follows:

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;
  }
}

The above is the detailed content of Briefly describe the commonly used time tool classes in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn