Java中日期操作

package com.zx.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
private static final String FORMAT="yyyy-MM-dd HH:mm:ss";

public static Date getTime(){
Calendar now=Calendar.getInstance();
return now.getTime();
}

public static String formatTime(Date time){
SimpleDateFormat sdf=new SimpleDateFormat(FORMAT);
return sdf.format(time);
}

public static String getCurrentTime(){
Calendar now=Calendar.getInstance();
StringBuffer time=new StringBuffer();
time.append(now.get(now.YEAR));
time.append(now.get(now.MONTH)+1);
time.append(now.get(now.DAY_OF_MONTH));
time.append(now.get(now.HOUR));
time.append(now.get(now.MINUTE));
time.append(now.get(now.SECOND));

return time.toString();

}

/**
* 判断是否在发短信的时间范围内(8:00-23:00) (penghuaiyi 2008-12-22)
* @return
*/
public static boolean isSendTime(){
boolean flag=false;

Calendar current=Calendar.getInstance();
int year=current.get(Calendar.YEAR);
int month=current.get(Calendar.MONTH);
int date=current.get(Calendar.DAY_OF_MONTH);
//设置短信发送开始时间:8:00
current.set(year, month, date, BEGIN_TIME,0,0);
Date beginTime=current.getTime();
//设置短信发送截止时间:23:00
current.set(year, month, date, END_TIME,0,0);
Date endTime=current.getTime();

Date now=new Date(); //当前时间
if(now.after(beginTime) && now.before(endTime))
{
flag=true;
}

return flag;
}


public static void main(String args[]){
Date date=DateUtil.getTime();
String time=DateUtil.formatTime(date);
System.out.println(time);

String str=DateUtil.getCurrentTime();
System.out.println(str);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值