Date类的一些应用

package tools;
/*日期的一些处理方法*/
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateOperation {
//判断日期是否过期
private boolean isExpire(String expiredate){
if(expiredate==null||expiredate.length()==0){
return false;
}
Date date=new Date();
Date tmp=null;
SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");//格式化日期
try{
tmp=sdf.parse(expiredate);
if(tmp.after(date)){//指定日期在现在日期之后,则为过期
return false;
}
}catch(Exception ex){
// logger.error("设置的过期日期["+expiredate+"]错误,系统默认过期");
}
return true;
}

public static void handDate(String validateTime) {
if (validateTime != null && !validateTime.equals("")) {
try {
String[] startAndEndDays = validateTime.split("-");
System.out.println(startAndEndDays[0]+"..."+startAndEndDays[1]);
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new Date());
int day = cal.get(GregorianCalendar.DAY_OF_MONTH);
System.out.println("当前时间:" + day);
if (day < Integer.parseInt(startAndEndDays[0])
|| day > Integer.parseInt(startAndEndDays[1])) {
// 当前时间不在有效时间段内
System.out.println("当前时间不在有效时间段内");
return;
}else{
System.out.println("当前时间在有效时间段内");
}
} catch (Exception e) {
System.out.println("传入的参数e解析出错,请按正确格式配置!");
}
}
}

/**
* 获取系统时间
*/
public static String getDate() {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String day = format.format(date);
return day;
}

/**
* 获取每月的第一天
*/
public static String getMonthOne() {
String day = getDate();
day = day.substring(0, 8) + "01";
return day;
}

/**
* 获取当前时间
*/
public static String getCurrentTime(){
Calendar cl= Calendar.getInstance();
cl.setTime(new java.util.Date());
Date date=cl.getTime();
SimpleDateFormat ft=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return ft.format(date);
}

/**
* 根据字符串获得时间
*/
public static Date string2Dte(String time){
SimpleDateFormat ft=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return ft.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return new Date();
}

/**
* 得到现在小时
*/
public static String getHour() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
String hour;
hour = dateString.substring(11, 13);
return hour;
}

public static void main(String[] args) {
System.out.println(getHour());
}

/**
* 得到现在分钟
*/
public static String getTime() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
String min;
min = dateString.substring(14, 16);
return min;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值