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

import java.util.*;

import java.text.*;

/**

* a DateTime 定义了日期时间的一些便捷的格式化操作

*

* @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 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());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值