---
title: "一个好用的日期时间类(java)收藏"
author: "fly1206"
date: "2010-08-26T17:42:14Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2010-08-26T17:42:14Z"
id: 748078
url: "https://fly1206.iteye.com/blog/748078"
---

import java.util.\*;  
import java.text.\*;

/\**  
 * a &lt;code&gt; DateTime &lt;/code&gt; 定义了日期时间的一些便捷的格式化操作  
 *   
 * @version 1.0  
 * @author markhuang  
 \*/  
public class DateTime {  
 /\**  
  * 存储时间和日期，默认当前时间和日期  
  \*/  
 private Calendar cale = Calendar.getInstance();

 /\**  
  * 默认构造函数,得到当前时间和日期  
  \*/  
 public DateTime() {  
  //  
 }

 /\**  
  * 构造函数,可设置毫秒  
  *   
  * @param millisecond  
  * 过去的毫秒数  
  * @see #DateTime  
  \*/  
 public DateTime(long millisecond) {  
  cale.setTimeInMillis(millisecond);  
 }

 /\**  
  * 构造函数,可设置年月日时分秒  
  *   
  * @param year  
  * 年  
  * @param month  
  * 月  
  * @param day  
  * 日  
  * @param hour  
  * 小时  
  * @param minute  
  * 分钟  
  * @param second  
  * 秒  
  * @see #DateTime(long)  
  \*/  
 public DateTime(int year, int month, int day, int hour, int minute,  
  int second) {  
  cale.set(Calendar.YEAR, year);  
  cale.set(Calendar.MONTH, month - 1);  
  cale.set(Calendar.DAY\_OF\_MONTH, day);  
  cale.set(Calendar.HOUR\_OF\_DAY, hour);  
  cale.set(Calendar.MINUTE, minute);  
  cale.set(Calendar.SECOND, second);  
 }

 /\**  
  * 得到当前年  
  *   
  * @return 当前年  
  \*/  
 public int year() {  
  return cale.get(Calendar.YEAR);  
 }

 /\**  
  * 得到当前月  
  *   
  * @return 当前月  
  \*/  
 public int month() {  
  return cale.get(Calendar.MONTH) + 1;  
 }

 /\**  
  * 得到当前日  
  *   
  * @return 当前日  
  \*/  
 public int day() {  
  return cale.get(Calendar.DAY\_OF\_MONTH);  
 }

 /\**  
  * 得到当前小时  
  *   
  * @return 当前小时  
  \*/  
 public int hour() {  
  return cale.get(Calendar.HOUR\_OF\_DAY);  
 }

 /\**  
  * 得到当前分钟  
  *   
  * @return 当前分钟  
  \*/  
 public int minute() {  
  return cale.get(Calendar.MINUTE);  
 }

 /\**  
  * 得到当前秒  
  *   
  * @return 当前秒  
  \*/  
 public int second() {  
  return cale.get(Calendar.SECOND);  
 }

 /\**  
  * 得到当前毫秒  
  *   
  * @return 当前毫秒  
  \*/  
 public int millisecond() {  
  return cale.get(Calendar.MILLISECOND);  
 }

 /\**  
  * 得到总毫秒数  
  *   
  * @return 总毫秒数  
  \*/  
 public long allmillisecond() {  
  return cale.getTimeInMillis();  
 }

 /\**  
  * 得到当前星期几  
  *   
  * @return 当前星期几  
  \*/  
 public int dayofweek() {  
  return cale.get(Calendar.DAY\_OF\_WEEK) - 1;  
 }

 /\**  
  * 得到当前是当年中的第几天  
  *   
  * @return 当年中的第几天  
  \*/  
 public int dayofyear() {  
  return cale.get(Calendar.DAY\_OF\_YEAR);  
 }

 /\**  
  * 得到yyyyMMddhhmmss 格式的日期  
  *   
  * @return yyyyMMddhhmmss 格式的日期  
  * @see #longdatetime(char, char)  
  \*/  
 public String shortdatetime() throws ArrayIndexOutOfBoundsException {  
  SimpleDateFormat format = null;  
  try {  
  format = new SimpleDateFormat("yyyyMMddhhmmss");  
  } catch (IllegalArgumentException e) {  
  throw new ArrayIndexOutOfBoundsException();  
  }  
  return format.format(cale.getTime());  
 }

 /\**  
  * 得到类似yyyy-MM-dd hh:mm:ss 格式的日期  
  *   
  * @param datesep  
  * 日期分隔符号  
  * @param timesep  
  * 时间分隔符号  
  * @return 类似yyyy-MM-dd hh:mm:ss 格式的日期  
  * @see #longdatelongtime(char, char)  
  \*/  
 public String longdatetime(char datesep, char timesep)  
  throws ArrayIndexOutOfBoundsException {  
  String pattern = "yyyy" + datesep + "MM" + datesep + "dd";  
  pattern += " hh" + timesep + "mm" + timesep + "ss";  
  SimpleDateFormat format = null;  
  try {  
  format = new SimpleDateFormat(pattern);  
  } catch (IllegalArgumentException e) {  
  throw new ArrayIndexOutOfBoundsException();  
  }  
  return format.format(cale.getTime());  
 }

 /\**  
  * 得到类似yyyy-MM-dd hh:mm:ss:SSS 格式的日期  
  *   
  * @param datesep  
  * 日期分隔符号  
  * @param timesep  
  * 时间分隔符号  
  * @return 类似yyyy-MM-dd hh:mm:ss:SSS 格式的日期  
  * @see #longdatetime(char, char)  
  \*/  
 public String longdatelongtime(char datesep, char timesep)  
  throws ArrayIndexOutOfBoundsException {  
  String pattern = "yyyy" + datesep + "MM" + datesep + "dd";  
  pattern += " hh" + timesep + "mm" + timesep + "ss";  
  pattern += timesep + "SSS";  
  SimpleDateFormat format = null;  
  try {  
  format = new SimpleDateFormat(pattern);  
  } catch (IllegalArgumentException e) {  
  throw new ArrayIndexOutOfBoundsException();  
  }  
  return format.format(cale.getTime());  
 }

 /\**  
  * 得到类似yyyyMMdd格式的日期  
  *   
  * @return 类似yyyyMMdd格式的日期  
  * @see #longdate(char)  
  \*/  
 public String shortdate() throws ArrayIndexOutOfBoundsException {  
  SimpleDateFormat format = null;  
  try {  
  format = new SimpleDateFormat("yyyyMMdd");  
  } catch (IllegalArgumentException e) {  
  throw new ArrayIndexOutOfBoundsException();  
  }  
  return format.format(cale.getTime());  
 }

 /\**  
  * @param datesep  
  * 日期分隔符号  
  * @return 类似yyyy-MM-dd格式的日期  
  * @see #shortdate()  
  \*/  
 public String longdate(char datesep) throws ArrayIndexOutOfBoundsException {  
  String pattern = "yyyy" + datesep + "MM" + datesep + "dd";  
  SimpleDateFormat format = null;  
  try {  
  format = new SimpleDateFormat(pattern);  
  } catch (IllegalArgumentException e) {  
  throw new ArrayIndexOutOfBoundsException();  
  }  
  return format.format(cale.getTime());  
 }

 /\**  
  * @param s  
  * 被输出到输出终端的值  
  \*/  
 public &lt;T&gt; void out(T s) {  
  System.out.println(s);  
 }

 public static void main(String\[] args) {  
  // TODO Auto-generated method stub

  try {  
  DateTime dt = new DateTime();  
  dt.out(dt.year());  
  dt.out(dt.month());  
  dt.out(dt.longdatetime('-', '&'));  
  dt.out(dt.longdatelongtime('-', ':'));  
  dt = new DateTime(2009, 5, 21, 12, 56, 12);  
  dt.out(dt.day());  
  dt.out(dt.hour());  
  dt.out(dt.longdatetime('-', '&'));  
  dt.out(dt.longdatelongtime('-', ':'));  
  dt.out(new DateTime().allmillisecond() - dt.allmillisecond());  
  dt = new DateTime(dt.allmillisecond() - 1000000000);  
  dt.out(dt.minute());  
  dt.out(dt.second());  
  dt.out(dt.longdatelongtime('-', ':'));  
  } catch (ArrayIndexOutOfBoundsException e) {  
  new DateTime().out(e.getMessage());  
  }  
 }  
}