import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
*
*
* @fileName DateUtil.java
* @author alex.tong
* @createDate 2010-11-1
*
* @modifyBy ...
* @modifyDate ...
* < ... Why & What is modified ... >
*
* @version 1.0
*/
public class DateUtil {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
private static SimpleDateFormat sfdate = new SimpleDateFormat("yyyy0MM");
private static SimpleDateFormat sftime = new SimpleDateFormat("yyyyMMddHHmmss");
private static SimpleDateFormat sfmonth = new SimpleDateFormat("yyyyMMDD");
private static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static SimpleDateFormat sdfYearMonth = new SimpleDateFormat("yyyy-MM");
/**
* methodName : 取指定日期的月份所在的季度值,并转换为月份,如2009-05-15 返回 06
* param :
* @param date 指定的日期
* @param formatValue 指定的掩码
* @return
* DateUtil
* @author alex.tong
* create on: Jul 8, 2009 10:36:04 AM
*/
public static String getMonth(Date date){
String resultMonth = "";
if(null!=date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH); ;
int quarterMonth = ((int)Math.floor(month/3)) * 3 + 3;
resultMonth = String.valueOf(quarterMonth);
if(quarterMonth<10){
resultMonth = "0" + String.valueOf(quarterMonth);
}
}
return resultMonth;
}
/**
* methodName : 返回指定日期 加减某个天数的日期
* param :
* @param date 日期
* @param addDay 需要增减的天数
* @return
* @throws Exception
* DateUtil
* @author philip
* create on: Jul 8, 2009 9:56:39 AM
*/
public static Date addDay(Date date,int addDay){
if(null!=date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, addDay);
return calendar.getTime();
}
return null;
}
/**
* 获得给定(当前)年份月份的 delMonth 后 年份月份
* 创建人:alex.tong
* 创建时间:Apr 13, 2010 11:13:06 AM
*/
public static String getYearMonth(String yearMonth,int delMonth){
Date date = new Date();
if(yearMonth != null){
try {
date = sdfYearMonth.parse(yearMonth) ;
} catch (ParseException e) {
date = new Date();
}
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, delMonth);
int month = calendar.get(Calendar.MONTH) + 1;
String monthStr = month + "";
if(month < 10){
monthStr = "0" + month;
}
return calendar.get(Calendar.YEAR) + "-" + monthStr;
}
/**
* 以yyyy0MM格式返回传入日期
*
* @param date
* @return
*/
public static String formPeriod(Date date) {
if (date != null) {
return sfdate.format(date);
}
return null;
}
/**
* 以yyyyMMddHHmmss格式返回传入日期
*
* @param date
* @return
*/
public static String formTime(Date date) {
if (date != null) {
return sftime.format(date);
}
return null;
}
/**
* 以yyyyMMDD格式返回传入日期
*
* @param date
* @return
*/
public static String formMonth(Date date) {
if (date != null) {
return sfmonth.format(date);
}
return null;
}
public static Date formDate(String year) {
Calendar calendar = Calendar.getInstance();
calendar.set(Integer.parseInt(year), 1, 1);
return calendar.getTime();
}
/**
* 根据传入年、月参数返回日期类型(1日)
*
* @param year
* @param month
* @return
*/
public static Date formDate(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
return calendar.getTime();
}
/**
* 根据传入年、月、日参数返回日期类型
*
* @param year
* @param month
* @param date
* @return
*/
public static Date formDate(int year, int month, int date) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, date, 0, 0, 0);
return calendar.getTime();
}
public static boolean isNotInDate(Date begindate, Date enddate) {
Date date = new Date();
if(begindate.getTime() < date.getTime() && enddate.getTime() > date.getTime()){
return false;
}
return true;
}
/**
* 取指定日期所在月最后一天
* @param date
* @return
*/
public static Date getLastDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
final int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DATE, lastDay);
return withoutTime(calendar.getTime());
}
/**
* 取指定日期所在月第一天
* @param date
* @return
*/
public static Date getFirstDayOfMonth(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
final int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DATE,firstDay);
return withoutTime(calendar.getTime());
}
/**
* 取出日期中的时分秒
* @param date
* @returnFri Jul 16 15:54:32 CST 2010
*/
public static Date withoutTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
return pareDate(year + "/" + month + "/" + day);
}
public static Date getYesterday() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DATE, -1);
return calendar.getTime();
}
/**
* 格式化为“yyyy-MM-dd”
*
* @param date
* @return
*/
public static String toFormatDate(Date date) {
if (null == date) {
return null;
}
return sdf.format(date);
}
/**
* 格式化为“yyyy-MM-dd”
*
* @param date
* @return
*/
public static String toFormatDateTime(Date date) {
if (null == date) {
return null;
}
return dateTimeFormat.format(date);
}
public static String getDateFormat(Date date , String format){
if(format == null){
format = "yyyy-MM-dd";
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.format(date);
}
/**
*
*<p>@author: alex.tong<p>
*<p>@createDate: Jan 7, 2010<p>
*<p>@method: getDate<p>
*<p>@param date
*<p>@param days
*<p>@return: <p>
*<p>@return: Date<p>
*<p>@TODO: 指定时间date,days天后的时间 <p>
*/
public static Date getDate(Date date, int days) {
long l = 24 * 3600 * 1000L * days + date.getTime();
return new Date(l);
}
/**
* 取日期的0点
* @param date
* @return
*/
public static Date getDateWithoutTime(){
return getDateWithoutTime(new Date());
}
public static Date getDateWithoutTime(Date date){
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
return date;
}
public static Date pareDate(String source) {
Date date = null;
try {
date = dateFormat.parse(source);
} catch (ParseException e1) {
try {
date = sdf.parse(source);
} catch (ParseException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
return date;
}
/**
* 取某年月最大天数
* @param year
* @param month
* @return
*/
public static int getDaysOfMonth(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
public static int getDay(Date date) {
if(date == null) date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DATE);
}
}
分享到:
相关推荐
An Attention-Based Unsupervised Adversarial Model for Movie Review Spam Dete.pdf
信息安全_数据安全_Malicious-Encrypted-Traffic-Dete 数字风险 数据脱敏 信息保护 漏洞分析 大数据
信息安全_数据安全_Enhancing_Deep_Learning_DGA_Dete 信息安全 大数据 安全设计 安全对抗 安全研究
An Empirical Review of Deep Learning Frameworks for Change Detection Model Design, Experimental Frameworks, Challenges and Research Needs.zip
What's new in v1.3.1 : [+]Support For WINDOWS SEVEN BUILD 7600 [+]Added Bugcheck(Reason) Callback Notifications Detection [+]Added Hidden DLLs Detection [+]Added New Features For DLLs (ZeroMemory/...
Opencv检测人脸,可以批量对某一个文件夹中(路径名不要有中文)所有文件操作,并将检测后的人脸抠出来保存到项目本地文件夹下.使用vc2005完成.
特征点提取和匹配。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。...
An Ensemble Detection Method for Shilling Attacks Based on Features of Automatic Extraction Yaojun Hao1,2,3, Fuzhi Zhang1,2,*, Jinbo Chao1,2
dete(i, j) = dete(i - 1, j - 1) + dete(i - 1, j) End If Next Next For i = 0 To n For j = 0 To i Debug.Write(dete(i, j) & " ") sum += dete(i, j) Next Debug.WriteLine(" ") Next Debug....
SwellKine.Dete1rminedBran1d.gaB6RrD
RaterKine.Dete1rminedBran1d.gam2aEY
一种模糊入侵检测算法,用于检测网络入侵数据
VB模块化编程声卡检测设计代码VB code for modular programming design of the sound card detection
sar 图像边缘检测算子,代码简洁,能够运行成功。
语言:한국어 在浏览器中设计测试:浏览器中的设计测试 在空白浏览器屏幕上插入文本,您可以查看大小,更改颜色,您可以查看颜色。< / p>
The Haar transform is the simplest of the wavelet transforms. This transform cross-multiplies a function against the Haar wavelet with various shifts and stretches, like the Fourier transform cross-...
自动假新闻检测方法在对抗性攻击中有多脆弱_How Vulnerable Are Automatic Fake News Detection Methods to Adversarial Attacks.pdf
该程序能够实现对一张图片中圆的识别,这也是别人的程序,为了下载借用下,不好意思了。
基于体素到点解码器的点云三维目标检测_From Voxel to Point IoU-guided 3D Object Detection for Point Cloud with Voxel-to-Point Decoder.pdf
深度学习领域yolov8橄榄球比赛NFL目标检测(带数据集)--nfl-yolov8-player-position-detection 语言:python 内容包括:源码、数据集、数据集描述、论文 目的:使用yolov8算法在橄榄球比赛中目标检测。...