java 整数转化为时间_JAVA实现日期+整数转换为日期的类

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

/**

* 该类主要完成对字符的验证和过滤

* @author

*

*/

public class StringUtil {

//转换日期格式

public  static  java.sql.Date   parseDate(String   s)throws   Exception{

String   []   str=s.split("-");

if(str.length!=3){

throw   new   Exception("日期格式不正确!");

}

int   year=Integer.parseInt(str[0]);

int   month=Integer.parseInt(str[1]);

int   day=Integer.parseInt(str[2]);

if(year>9999   ||   year<0){

throw   new   Exception("日期格式不正确!");

}

if(month<1   ||   month>12){

throw   new   Exception("日期格式不正确!");

}

if(day<1   ||   day>31){

throw   new   Exception("日期格式不正确!");

}

return   new   java.sql.Date(year-1900,month-1,day);

}

/**

* 把字符串解析为Date对象

* @param str 日期字符串

* @return 日期对象

* @throws ParseException

*/

public static java.util.Date StrToDate(String str) throws ParseException {

//指定日期格式

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

java.util.Date date = null;

//把字符串解析为Date对象

date = format.parse(str);

return date;

}

/**

* 根据mar是几月返回一个日期类型对象

* @param date 日期对象

* @return 字符串

*/

public static String DateToStr(java.util.Date date) {

//指定日期格式

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//把日期对象解析为指定格式的字符串

String str = format.format(date);

return str;

}

/**

* 日期+整数=日期  (分钟数)

* @param dt 日期

* @param count  整数

* @return 日期

*/

public static java.sql.Date MINUTE_Shift(java.util.Date dt, int count)

{

Calendar cal = Calendar.getInstance();

//使用给定的 Date 设置此 Calendar 的时间

cal.setTime(dt);

//根据日历的规则,为给定的日历字段添加或减去指定的时间量

//Calendar.MINUTE,用来设置 整数与日期中的 什么相加 Calendar.MINUTE为钟

cal.add(Calendar.MINUTE, count);

return new java.sql.Date(cal.getTimeInMillis());

//java.sql.Date(cal.getTimeInMillis()).toString()  // 默认格式化日期转义形式 yyyy-mm-dd 的日期

}

/*  这个能看出效果

public static java.util.Date MINUTE_Shift(java.util.Date dt, int count)

{

Calendar cal = Calendar.getInstance();

//使用给定的 Date 设置此 Calendar 的时间

cal.setTime(dt);

//根据日历的规则,为给定的日历字段添加或减去指定的时间量

//Calendar.MINUTE,用来设置 整数与日期中的 什么相加 Calendar.MINUTE为钟

cal.add(Calendar.MINUTE, count);

return new java.util.Date (cal.getTimeInMillis());

}

*/

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值