import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar; /**
* DATE 继承于 java.util.Date,多实现了很多方法。
* @author 沙琪玛 QQ:862990787
* May 29, 2013 9:52:51 AM
*/
public class Date extends java.util.Date { /**
*
*/
private static final long serialVersionUID = 2155545266875552658L; /**
* 功能:转换为Calendar。
* @author 沙琪玛 QQ:862990787
* Aug 21, 2013 8:58:31 AM
* @return Calendar
*/
public Calendar toCalendar() {
Calendar c = Calendar.getInstance();
c.setTime(this);
return c;
} /**
* 功能:判断日期是否和当前date对象在同一天。
* @author 沙琪玛 QQ:862990787
* Aug 21, 2013 7:15:53 AM
* @param date 比较的日期
* @return boolean 如果在返回true,否则返回false。
*/
public boolean isSameDay(Date date) {
if (date == null) {
throw new IllegalArgumentException("日期不能为null");
}
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date);
return this.isSameDay(cal2);
} /**
* 功能:判断日期是否和当前date对象在同一天。
* @author 沙琪玛 QQ:862990787
* Aug 21, 2013 7:15:53 AM
* @param cal 比较的日期
* @return boolean 如果在返回true,否则返回false。
*/
public boolean isSameDay(Calendar cal) {
if (cal == null) {
throw new IllegalArgumentException("日期不能为null");
}
//当前date对象的时间
Calendar cal1 = Calendar.getInstance();
cal1.setTime(this);
return (cal1.get(Calendar.ERA) == cal.get(Calendar.ERA) &&
cal1.get(Calendar.YEAR) == cal.get(Calendar.YEAR) &&
cal1.get(Calendar.DAY_OF_YEAR) == cal.get(Calendar.DAY_OF_YEAR));
} /**
* 功能:将当前日期的秒数进行重新设置。
* @author 沙琪玛 QQ:862990787
* Jul 31, 2013 2:42:36 PM
* @param second 秒数
* @return 设置后的日期
*/
public Date setSecondNew(int second){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.SECOND,second);
return new Date(c.getTimeInMillis());
} /**
* 功能:将当前日期的分钟进行重新设置。
* @author 沙琪玛 QQ:862990787
* Jul 31, 2013 2:42:36 PM
* @param minute 分钟数
* @return 设置后的日期
*/
public Date setMinuteNew(int minute){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.MINUTE,minute);
return new Date(c.getTimeInMillis());
} /**
* 功能:将当前日期的小时进行重新设置。
* @author 沙琪玛 QQ:862990787
* Jul 31, 2013 2:42:36 PM
* @param hours 小时数 (24小时制)
* @return 设置后的日期
*/
public Date setHourNew(int hour){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.HOUR_OF_DAY, hour);
return new Date(c.getTimeInMillis());
} /**
* 功能:将当前日期的天进行重新设置。
* @author 沙琪玛 QQ:862990787
* Jul 31, 2013 2:42:36 PM
* @param days 某一天
* @return 设置后的日期
*/
public Date setDayNew(int day){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.DATE,day);
return new Date(c.getTimeInMillis());
} /**
* 功能:将当前日期的月进行重新设置。
* @author 沙琪玛 QQ:862990787
* Jul 31, 2013 2:42:36 PM
* @param months 某一月
* @return 设置后的日期
*/
public Date setMonthNew(int month){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.MONTH, month-1);
return new Date(c.getTimeInMillis());
} /**
* 功能:将当前日期的年进行重新设置。
* @author 沙琪玛 QQ:862990787
* Jul 31, 2013 2:42:36 PM
* @param years 某一年
* @return 设置后的日期
*/
public Date setYearNew(int year){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.YEAR, year);
return new Date(c.getTimeInMillis());
} /**
* 功能:得到当月有多少天。
* @author 沙琪玛 QQ:862990787
* Jul 2, 2013 4:59:41 PM
* @return int
*/
public int daysNumOfMonth(){
Calendar cal = Calendar.getInstance();
cal.setTime(this);
return cal.getActualMaximum(Calendar.DATE);
} /**
* 将yyyy-MM-dd HH:mm:ss字符串转换成日期(net.maxt.util.Date)<br/>
* @param dateStr 时间字符串
* @param 当前时间字符串的格式。
* @return net.maxt.util.Date 日期 ,转换异常时返回null。
*/
public static Date parseDate(String dateStr,SimpleDateFormat dataFormat){
try {
java.util.Date d = dataFormat.parse(dateStr);
return new Date(d.getTime());
} catch (ParseException e) {
e.printStackTrace();
return null;
}
} /**
* 将yyyy-MM-dd HH:mm:ss字符串转换成日期(net.maxt.util.Date)<br/>
* @param dateStr yyyy-MM-dd HH:mm:ss字符串
* @return net.maxt.util.Date 日期 ,转换异常时返回null。
*/
public static Date parseDate(String dateStr){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
java.util.Date d = sdf.parse(dateStr);
return new Date(d.getTime());
} catch (ParseException e) {
e.printStackTrace();
return null;
}
} /**
* 功能:计算两个时间的时间差。
* @author 沙琪玛 QQ:862990787
* May 29, 2013 2:34:08 PM
* @param time 另一个时间。
* @return Timespan 时间间隔
*/
public Timespan substract(Date time){
return new Timespan(this.getTime()-time.getTime());
} /**
* 功能:当前时间增加毫秒数。
* @author 沙琪玛 QQ:862990787
* May 29, 2013 11:26:27 AM
* @param milliseconds 正值时时间延后,负值时时间提前。
* @return Date
*/
public Date addMilliseconds(int milliseconds){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.MILLISECOND, c.get(Calendar.MILLISECOND)+milliseconds);
return new Date(c.getTimeInMillis());
} /**
* 功能:当前时间增加秒数。
* @author 沙琪玛 QQ:862990787
* May 29, 2013 11:26:27 AM
* @param seconds 正值时时间延后,负值时时间提前。
* @return Date
*/
public Date addSeconds(int seconds){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.SECOND, c.get(Calendar.SECOND)+seconds);
return new Date(c.getTimeInMillis());
} /**
* 功能:当前时间增加分钟数。
* @author 沙琪玛 QQ:862990787
* May 29, 2013 11:26:27 AM
* @param minutes 正值时时间延后,负值时时间提前。
* @return Date
*/
public Date addMinutes(int minutes){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.MINUTE, c.get(Calendar.MINUTE)+minutes);
return new Date(c.getTimeInMillis());
} /**
* 功能:当前时间增加小时数。
* @author 沙琪玛 QQ:862990787
* May 29, 2013 11:26:27 AM
* @param hours 正值时时间延后,负值时时间提前。
* @return Date
*/
public Date addHours(int hours){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.HOUR, c.get(Calendar.HOUR)+hours);
return new Date(c.getTimeInMillis());
} /**
* 功能:当前时间增加天数。
* @author 沙琪玛 QQ:862990787
* May 29, 2013 11:26:27 AM
* @param days 正值时时间延后,负值时时间提前。
* @return Date
*/
public Date addDays(int days){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.DATE, c.get(Calendar.DATE)+days);
return new Date(c.getTimeInMillis());
} /**
* 功能:当前时间增加月数。
* @author 沙琪玛 QQ:862990787
* May 29, 2013 11:26:27 AM
* @param months 正值时时间延后,负值时时间提前。
* @return Date
*/
public Date addMonths(int months){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.MONTH, c.get(Calendar.MONTH)+months);
return new Date(c.getTimeInMillis());
} /**
* 功能:当前时间增加年数。注意遇到2月29日情况,系统会自动延后或者减少一天。
* @author 沙琪玛 QQ:862990787
* May 29, 2013 11:26:27 AM
* @param years 正值时时间延后,负值时时间提前。
* @return Date
*/
public Date addYears(int years){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.YEAR, c.get(Calendar.YEAR)+years);
return new Date(c.getTimeInMillis());
} /**
* 得到秒。格式:56<br/>
* @return int
*/
public int secondInt() {
return Integer.parseInt(toString("ss"));
} /**
* 得到分钟。格式:56<br/>
* @return int
*/
public int minuteInt() {
return Integer.parseInt(toString("mm"));
} /**
* 得到小时。格式:23<br/>
* @return int
*/
public int hourInt() {
return Integer.parseInt(toString("HH"));
} /**
* 得到日。格式:26<br/>
* 注意:这里1日返回1,2日返回2。
* @return int
*/
public int dayInt() {
return Integer.parseInt(toString("dd"));
} /**
* 得到月。格式:5<br/>
* 注意:这里1月返回1,2月返回2。
* @return int
*/
public int monthInt() {
return Integer.parseInt(toString("MM"));
} /**
* 得到年。格式:2013
* @return int
*/
public int yearInt() {
return Integer.parseInt(toString("yyyy"));
} /**
* 得到短时间。格式:12:01
* @return String
*/
public String shortTime() {
return toString("HH:mm");
} /**
* 得到长时间。格式:12:01:01
* @return String
*/
public String longTime() {
return toString("HH:mm:ss");
} /**
* 得到今天的第一秒的时间。
* @return Date
*/
public Date dayStart() {
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
return new Date(c.getTimeInMillis());
} /**
* 得到当前所在自然月的第一天的开始,格式为长日期格式。例如:2012-03-01 00:00:00。
* @return Date
*/
public Date monthStart(){
Calendar c=Calendar.getInstance();
String startStr= toString("yyyy-M-")+c.getActualMinimum(Calendar.DATE)+" 00:00:00";
return Date.parseDate(startStr);
} /**
* 得到今天的最后一秒的时间。
* @return Date
*/
public Date dayEnd() {
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
return new Date(c.getTimeInMillis());
} /**
* 根据日期得到星期几,得到数字。<br/>
* 7, 1, 2, 3, 4, 5, 6
* @return Integer 如:6
*/
public int dayOfWeekInt() {
Integer dayNames[] = { 7, 1, 2, 3, 4, 5, 6 };
Calendar calendar = Calendar.getInstance();
calendar.setTime(this);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
if (dayOfWeek < 0)
dayOfWeek = 0;
return dayNames[dayOfWeek];
} /**
* 将日期转换成长日期字符串 例如:2009-09-09 01:01:01
* @return String
*/
public String toLongDate() {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return (null == this) ? null : df.format(this);
} /**
* 将日期按照一定的格式进行格式化为字符串。<br/>
* 例如想将时间格式化为2012-03-05 12:56 ,则只需要传入formate为yyyy-MM-dd HH:mm即可。
* @param formate 格式化格式,如:yyyy-MM-dd HH:mm
* @return String 格式后的日期字符串。如果当前对象为null,则直接返回null。
*/
public String toString(String formate) {
DateFormat df = new SimpleDateFormat(formate);
return (null == this) ? null : df.format(this);
} /**
* 得到某个时间的时间戳yyyyMMddHHmmss。
* @param date 时间
* @return String 如果当前对象为null,则直接返回null。
*/
public String toTimeStamp() {
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
return (null == this) ? null : df.format(this);
} /**
* 将日期转换成短日期字符串,例如:2009-09-09。
* @return String ,如果当前对象为null,返回null。
*/
public String toShortDate() {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return (null == this) ? null : df.format(this);
} /**
* 功能:用java.util.Date进行构造。
* @author 沙琪玛 QQ:862990787
* May 29, 2013 10:59:05 AM
* @param java.util.Date date
*/
public Date(java.util.Date date) {
super(date.getTime());
} /**
* 功能:用毫秒进行构造。
* @author 沙琪玛 QQ:862990787
* May 29, 2013 10:59:05 AM
* @param timeInMillis
*/
public Date(long timeInMillis) {
super(timeInMillis);
} /**
* 功能:默认构造函数。
* @author 沙琪玛 QQ:862990787
* May 29, 2013 11:00:05 AM
*/
public Date() {
super();
} }

JAVA 文本 TXT 操作工具类的更多相关文章

  1. java/javascript 时间操作工具类

    一.java 时间操作工具类 import org.springframework.util.StringUtils; import java.text.ParseException; import ...

  2. java:数组操作工具类 java.util.Arrays包 主要方法详解

    Arrays类位于Java.util包下,是一个对数组操作的工具类,现将Arrays类中的方法做一个总结(JDK版本:1.6.0_34).Arrays类中的方法可以分为八类: sort(对数组排序) ...

  3. 170404、java版ftp操作工具类

    package com.rick.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotF ...

  4. 170403、java 版cookie操作工具类

    package com.rick.utils; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; imp ...

  5. Java IO(文件操作工具类)

    FileOperate实现的功能: 1. 返回文件夹中所有文件列表 2. 读取文本文件内容 3. 新建目录 4. 新建多级目录 5. 新建文件 6. 有编码方式的创建文件 7. 删除文件 8. 删除指 ...

  6. java 字符串String操作工具类

    /** * StrKit. */ public class StrKit { /** * 首字母变小写 */ public static String firstCharToLowerCase(Str ...

  7. JAVA文件操作工具类(读、增、删除、复制)

    使用JAVA的JFinal框架 1.上传文件模型类UploadFile /** * Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com). * ...

  8. docker 部署vsftpd服务、验证及java ftp操作工具类

    docker部署vsftpd服务 新建ftp文件存储目录/home/ftp cd /home mkdir ftp 创建一个组,用于存放ftp用户 groupadd ftpgroups 创建ftp用户, ...

  9. 拼音操作工具类 - PinyinUtil.java

    拼音操作工具类,提供字符串转换成拼音数组.汉字转换成拼音.取汉字的首字母等方法. 源码如下:(点击下载 -PinyinUtil.java.pinyin4j-2.5.0.jar ) import net ...

随机推荐

  1. python中__dict__与dir()的区别

    在python中__dict__与dir()都可以返回一个对象的属性,区别在于: __dict__是对象的一个属性,而dir()是一个built-in的方法: __dict__返回一个对象的属性名和值 ...

  2. 用socket发送匿名邮件之python实现

    发送邮件可以用smtp协议,整个过程为: 用户代理(user-agent,比如outlook.foxmail等邮件客户端)---(smtp协议)--->本地邮件服务器 --- (smtp协议)- ...

  3. Diffie–Hellman key exchange

    General overview[edit]   Illustration of the idea behind Diffie–Hellman key exchange Diffie–Hellman ...

  4. Android逆向之旅---反编译利器Apktool和Jadx源码分析以及错误纠正

    Android逆向之旅---反编译利器Apktool和Jadx源码分析以及错误纠正 http://blog.csdn.net/jiangwei0910410003/article/details/51 ...

  5. mvc bundle的介绍及使用 转载自 http://www.ityouzi.com/archives/mvc-bundleconfig.html

    Asp.Net MVC4 BundleConfig文件合并.压缩,网站优化加速 浏览器在向服务器发送请求的时候,请求的文件链接数量是有限制的,如果页面文件少就没有什么问题了,如果文件太多就会导致链接失 ...

  6. 【Sql Server】Sql语句整理

    use Person <--添加约束--> Alter table Student alter column Sno ) not null; Alter table Student Add ...

  7. 编译PHP并与Ngnix整合

    nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端. nginx一般是把请求发fastcgi管理进程处理,fascgi管 ...

  8. Jquery 官网下载流程

    选中解压版本,然后把ctrl+s保存另存为他的min版本

  9. Mybatis源码分析之参数处理

    Mybatis对参数的处理是值得推敲的,不然在使用的过程中对发生的一系列错误直接懵逼了. 以前遇到参数绑定相关的错误我就是直接给加@param注解,也稀里糊涂地解决了,但是后来遇到了一些问题推翻了我的 ...

  10. spring中使用@Value设置全局变量默认值

    前几天在开发过程中遇到一个使用 spring 的 @Value 给类的全局变量设置默认值不成功的问题,最后通过查资料也是轻松解决,但是发现使用@Value也是有多种多样的方式,今天总算是将开发任务结束 ...