`
fly1206
  • 浏览: 55527 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多


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

/**
 * a <code> DateTime </code> 定义了日期时间的一些便捷的格式化操作
 * 
 * @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 <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.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());
  }
 }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics