在java中处理日期我觉得不方便,需要引入好几个包Date和SimpleDateFormat或者DateFormat,而且Date设置日期我觉得很不方便,为了方便操作Date我写了一个类名为FlexDate,在类里处理日期、获取日期,我是觉得写一个类很方便,下面是代码,如果觉得哪里写的不好还请朋友们多多指教。
package org.fengdong;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by IntelliJ IDEA.
* ClassName:日期扩展类
* User: 冯东
* Date: 2007-9-9
* Time: 10:28:55
* To change this template use File | Settings | File Templates.
*/
public class FlexDate extends Date {
private SimpleDateFormat simpleDateFormat;
private Date date;
public FlexDate() {
this(new Date());
}
public FlexDate(Date date) {
this(date, "yyyy-MM-dd");
}
public FlexDate(Date date, String formatPattern) {
this.simpleDateFormat = new SimpleDateFormat(formatPattern);
this.date = new Date();
try {
this.date = this.simpleDateFormat.parse(this.simpleDateFormat.format(date));
}
catch (Exception exp) {
exp.printStackTrace();
}
}
/* public FlexDate(String dateString) {
String splitString = dateString.substring(3,4);
String formatPatten = "";
System.out.println(splitString);
if(splitString.equals("/") || splitString.equals("-"))
{
formatPatten = "yyyy" + splitString + "MM" + splitString + "dd";
}
else
{
formatPatten = "yyyyMMdd";
}
this(dateString, formatPatten);
}*/
/*
* 根据日期格式字符串和格式化字符串初始化
* */
public FlexDate(String dateString, String formatPattern) {
this.simpleDateFormat = new SimpleDateFormat(formatPattern);
date = new Date();
try {
date = this.simpleDateFormat.parse(dateString);
}
catch (Exception exp) {
exp.printStackTrace();
}
}
/*
* 获取日期格式化字符串
* */
public String getFormatePattern() {
return this.simpleDateFormat.toPattern();
}
/*
* 设置格日期格式化字符串
* */
public void setFormatePattern(String formatString) {
this.simpleDateFormat.applyPattern(formatString);
}
/*
*设置日期格式的字符串
* @para dateString
* 注意:此字符串的格式必须与设置的FormatPatten一致
*/
public void setDateString(String dateString) {
try {
this.date = this.simpleDateFormat.parse(dateString);
}
catch (Exception exp) {
exp.printStackTrace();
}
}
/*
* 返回当前设置日期的字符串格式
* */
public String getDateString()
{
return this.simpleDateFormat.format(this.date);
}
/*
* 获取设置的时间对象
* */
public Date getFlexDate() {
return date;
}
/*
* 设置时间对象
* */
public void setDate(Date date) {
this.date = date;
}
/*
* 返回日期字符串
* */
public String toString()
{
return getDateString();
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by IntelliJ IDEA.
* ClassName:日期扩展类
* User: 冯东
* Date: 2007-9-9
* Time: 10:28:55
* To change this template use File | Settings | File Templates.
*/
public class FlexDate extends Date {
private SimpleDateFormat simpleDateFormat;
private Date date;
public FlexDate() {
this(new Date());
}
public FlexDate(Date date) {
this(date, "yyyy-MM-dd");
}
public FlexDate(Date date, String formatPattern) {
this.simpleDateFormat = new SimpleDateFormat(formatPattern);
this.date = new Date();
try {
this.date = this.simpleDateFormat.parse(this.simpleDateFormat.format(date));
}
catch (Exception exp) {
exp.printStackTrace();
}
}
/* public FlexDate(String dateString) {
String splitString = dateString.substring(3,4);
String formatPatten = "";
System.out.println(splitString);
if(splitString.equals("/") || splitString.equals("-"))
{
formatPatten = "yyyy" + splitString + "MM" + splitString + "dd";
}
else
{
formatPatten = "yyyyMMdd";
}
this(dateString, formatPatten);
}*/
/*
* 根据日期格式字符串和格式化字符串初始化
* */
public FlexDate(String dateString, String formatPattern) {
this.simpleDateFormat = new SimpleDateFormat(formatPattern);
date = new Date();
try {
date = this.simpleDateFormat.parse(dateString);
}
catch (Exception exp) {
exp.printStackTrace();
}
}
/*
* 获取日期格式化字符串
* */
public String getFormatePattern() {
return this.simpleDateFormat.toPattern();
}
/*
* 设置格日期格式化字符串
* */
public void setFormatePattern(String formatString) {
this.simpleDateFormat.applyPattern(formatString);
}
/*
*设置日期格式的字符串
* @para dateString
* 注意:此字符串的格式必须与设置的FormatPatten一致
*/
public void setDateString(String dateString) {
try {
this.date = this.simpleDateFormat.parse(dateString);
}
catch (Exception exp) {
exp.printStackTrace();
}
}
/*
* 返回当前设置日期的字符串格式
* */
public String getDateString()
{
return this.simpleDateFormat.format(this.date);
}
/*
* 获取设置的时间对象
* */
public Date getFlexDate() {
return date;
}
/*
* 设置时间对象
* */
public void setDate(Date date) {
this.date = date;
}
/*
* 返回日期字符串
* */
public String toString()
{
return getDateString();
}
}