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());
}
}
}
分享到:
相关推荐
Java时间日期处理相关文档收藏 Java时间日期处理 Java日期处理
精品专题课件(2021-2022年收藏)
毕业设计,基于SSM+MyBatis+MySql+Bootstrap开发的新闻发布系统网站,内含Java完整源代码,数据库脚本 基于SSM新闻发布系统网站...新闻收藏: 收藏id,被收藏新闻,收藏人,收藏时间 新闻赞: 赞id,被赞新闻,用户,被赞时间
## 设计一个基于微信小程菜单查询app,一共有2个身份包括用户和管理员!用户通过微信小程序打开app后可以查询最新上架的菜谱信息,查询和查看菜谱详情信息,收藏自己喜欢的菜谱,发布留言和查看新闻公告!管理员可以...
设计一个基于微信小程菜单查询app,一共有2个身份包括用户和管理员!用户通过微信小程序打开app后可以查询最新上架的菜谱信息,查询和查看菜谱详情信息,收藏自己喜欢的菜谱,发布留言和查看新闻公告!管理员可以在...
每个项目都有一个提交日期,教师可以统计分析每一级的项目的数量、不同时间段(提交日期所处时间段)各级项目的数量,最好能有图表形式+列表形式(项目名称、提交人、等级),教师可点击查看,查看项目详情(学生...
设计一个基于微信小程菜单查询app,一共有2个身份包括用户和管理员!用户通过微信小程序打开app后可以查询最新上架的菜谱信息,查询和查看菜谱详情信息,收藏自己喜欢的菜谱,发布留言和查看新闻公告!管理员可以在...
前台框架: Bootstrap(一个HTML5响应式框架) 开发环境:myEclipse/Eclipse/Idea都可以 + mysql数据库 后台框架: SSM(SpringMVC + Spring + Mybatis) (1)用户者:用户在前台可以实现最基本的登陆注册,以及修改...
目标:用Java构建一个应用程序,以帮助收集器跟踪其收集中的项目。 Finn需要帮助来跟踪她的收藏。 她想查看她拥有哪些物品以及她在这些物品上花费了多少钱。 她需要能够: MVP列出她收藏中的所有物品获取每个物品...
【资源说明】 该项目是个人毕设项目源码,综合评分达到95分,经过严格调试,确保可以运行!放心下载使用。 该项目资源主要针对计算机、自动化等相关专业的学生或从业者...宝贝收藏: 收藏id,收藏宝贝,收藏用户,收藏时间
设计一个基于微信小程菜单查询app,一共有2个身份包括用户和管理员!用户通过微信小程序打开app后可以查询最新上架的菜谱信息,查询和查看菜谱详情信息,收藏自己喜欢的菜谱,发布留言和查看新闻公告!管理员可以在...
一个结构不能从另一个结构或类继承,而且不能作为一个类的基。但是,结构从基类 Object 继承。结构可实现接口,其方式同类完全一样。 [c ] 与 C 不同,无法使用 struct 关键字声明类。在 C# 中,类与结构在语义上是...
JSP基于SSM新闻发布系统网站设计毕业源码案例设计。 JSP基于SSM新闻发布系统网站设计 新闻分类: 分类id,分类名称 ...新闻收藏: 收藏id,被收藏新闻,收藏人,收藏时间 新闻赞: 赞id,被赞新闻,用户,被赞时间
日期和时间 依赖注入和AOP 控制台和命令行 函数式编程 反应式编程 安全与认证 高性能 序列化和 I/O 记录 Bean 映射和验证 7. 图像和视频 8. 代码生成和改变字节码 9. 分布式应用程序 10. 科学 11.OSGI 二. 数据库、...
课程信息,在课程信息页面可以查看课程名称,类型,上课时间,上课地点,课程简介,重点难点,工号,教师姓名等详细内容,并进行评论和收藏操作,如图5-3所示。 图5-3课程信息界面图 作业信息,在作业信息页面...
## 前台框架: Bootstrap(一个HTML5响应式框架) ## 开发环境:myEclipse/Eclipse/Idea都可以 + mysql数据库 ## 后台框架: SSM(SpringMVC + Spring + Mybatis) (1)用户者:用户在前台可以实现最基本的登陆注册,...
时间、日期相关 utils 打包 监测app状态、网络状态等变化 事件分发、手势识别等 --- 专业左右侧滑,可控制swipeEnable --- 代码家,上下左右各种滑:grinning_face_with_smiling_eyes: 文件存储 屏幕适配 自定义View ...
## 前台框架: Bootstrap(一个HTML5响应式框架) ## 开发环境:myEclipse/Eclipse/Idea都可以 + mysql数据库 ## 后台框架: SSM(SpringMVC + Spring + Mybatis) (1)用户者:用户在前台可以实现最基本的登陆注册,...