Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29943807
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2008-10-14 15:34:58

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
*


* 用于时间操作的方法
*


* @author

*/
public class Date {
/**
* 模式字符串
*/
public static final String short_pattern = "yyyy-mm-dd";
/**
* 模式字符串 yyyy-mm-dd hh:mm:ss
*/
public static final String long_pattern = "yyyy-mm-dd hh:mm:ss";
/**
* 模式字符串 yyyy年mm月dd日
*/
public static final String zh_short_pattern = "yyyy年mm月dd日";
/**
* 模式字符串 yyyy年mm月dd日 hh时mm分ss秒
*/
public static final String zh_long_pattern = "yyyy年mm月dd日 hh时mm分ss秒";
/**
* 模式字符串 yyyy年mm月dd日 hh时mm分ss秒 E
*/
public static final String zh_long_week_pattern = "yyyy年mm月dd日 hh时mm分ss秒 E";

/**
* 以与语言环境相关的方式来格式化和分析日期的具体类
*/
private static final SimpleDateFormat _format = new SimpleDateFormat("yyyy-mm-dd");
/**
* java 系统时间类
*/
private static java.util.Date _date = new java.util.Date();

/**
* 默认构造函数
*/
public Date(){
  
}
/**
* 用指定的时间对象初始化
*/
public Date(java.util.Date date){
   _date = date;
}

public Date(String date){
   try {
    _date = _format.parse(date);
   } catch (ParseException e) {
    _date = null;
   }
}
/**
*


* 按给定的模式字符串和时间字符串创建对象
*


* @param date 时间字符串
* @param pattern 模式字符串
*/
public Date(String date, String pattern){
   try {
    _date = (new SimpleDateFormat(pattern)).parse(date);
   } catch (ParseException e) {
    _date = null;
   }
}

public static java.util.Date getDate(){  
   return _date;
}
/**
*


* 按默认模式将给定的时间字符串转换成与其等值的java.util.Date对象
*


* @param date 时间字符串
* @return 转换后的java.util.Date类型的对象
*/
public static java.util.Date getDate(String date) {
   return getDate(date, short_pattern);
}

public static java.util.Date getDate(String date, String pattern){
   try {
    return (new SimpleDateFormat(pattern)).parse(date);
   } catch (ParseException e) {
    return null;
   }
}

/**
*


* 将当前系统时间按默认模式(yyyy-mm-dd)转换成与其等值的字符串形式
*


* @return 当前时间的字符串表示形式,格式为:yyyy-mm-dd
*/
public static String getDateString(){
   return getDateString(short_pattern);
}
/**
*


* 将指定时间按默认模式(yyyy-mm-dd)转换成与其等值的字符串形式
*


* @param date 要转换成字符串的时间
* @return 指定时间的字符串表示形式,格式为:yyyy-mm-dd
*/
public static String getDateString(java.util.Date date){
   return getDateString(date, short_pattern);
}
/**
*


* 根据指定的模式格式化当前时间
*


* @param pattern
* @return
*/
public static String getDateString(String pattern){
   return getDateString(_date, pattern);
}
/**
*


* 将指定时间按指定模式转换成与其等值的字符串形式
*


* @param date 要转换成字符串的时间
* @param pattern 将时间转换成字符串时使用的模式
* @return
*/
public static String getDateString(java.util.Date date, String pattern){
   return (new SimpleDateFormat(pattern)).format(date);
}

/**
* 取得当前对象的字符串表式形式
*
模式字符串 yyyy-mm-dd hh:mm:ss
*/
public static String getLangDateString(){
   return getDateString(long_pattern);
}
/**
* 取得当前对象的字符串表式形式
*
模式字符串 yyyy年mm月dd日
*/
public static String getZhShortDateString(){
   return getDateString(zh_short_pattern);
}
/**
* 取得当前对象的字符串表式形式
*
模式字符串 yyyy年mm月dd日 hh时mm分ss秒
*/
public static String getZhLangDateString(){
   return getDateString(zh_long_pattern);
}
/**
* 取得当前对象的字符串表式形式
*
模式字符串 yyyy年mm月dd日 hh时mm分ss秒 E
*/
public static String getZhLangWeekDateString(){
   return getDateString(zh_long_week_pattern);
}
}

阅读(1813) | 评论(0) | 转发(0) |
0

上一篇:简单尝试Google的Guice

下一篇:Guice

给主人留下些什么吧!~~