现在的位置: 首页 > 综合 > 正文

一个好用的日期时间类(java)

2014年02月18日 ⁄ 综合 ⁄ 共 5455字 ⁄ 字号 评论关闭

package util;

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

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

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

 /**
  * 构造函数,可设置毫秒
  *
  * @param millisecond
  *            过去的毫秒数
  * @see #DateTime
  */
 public DateTime(long millisecond) {
  cale.setLenient(false);
  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.setLenient(false);
  cale.set(Calendar.YEAR, year);
  // 老外的月份是从0到 11
  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() {
  // 老外的月份是从0到 11
  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() {
  // 老外星期天是一周的第一天
  int dayofweek = cale.get(Calendar.DAY_OF_WEEK);
  return dayofweek == 1 ? 7 : dayofweek - 1;
 }

 /**
  * 得到当前星期几
  *
  * @return 当前星期几
  */
 public String dayofweek_CN() throws ArrayIndexOutOfBoundsException {
  // 老外星期天是一周的第一天
  String[] dayofweek = { "日", "一", "二", "三", "四", "五", "六" };
  return "星期" + dayofweek[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 <T> 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.shortdatetime());
   dt.out(dt.dayofyear());
   dt.out(dt.day());
   dt.out(dt.month());
   dt.out(dt.dayofweek_CN());
   dt.out(dt.longdatetime('-', '&'));
   dt.out(dt.longdatelongtime('-', ':'));
   dt = new DateTime(2009, 5, 9, 12, 56, 12);
   dt.out("aa:" + dt.longdatelongtime('-', ':'));
   dt.out(dt.hour());
   dt.out(dt.shortdatetime());
   dt.out(dt.shortdate());
   dt.out(dt.dayofweek());
   dt.out(dt.dayofyear());
   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("exception:" + e.getMessage());
  } catch (java.lang.IllegalArgumentException e) {
   new DateTime().out("exception:" + e.getMessage());
  }
 }
}

【上篇】
【下篇】

抱歉!评论已关闭.