📄 dateutil.java
字号:
package cn.myapps.util;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* <p>
* Title: Cyberway Commons
* </p>
* <p>
* Description: Common Date Utility
* </p>
* <p>
* Copyright: Copyright (c) 2003
* </p>
* <p>
* Company: Cyberway Compucomm Co., Ltd.
* </p>
*
* @author Gaven
* @version 1.0
*/
public class DateUtil {
/**
* getDateStr get a string with format YYYY-MM-DD from a Date object
*
* @param date
* date
* @return String
*/
static public String getDateStr(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(date);
}
static public String getDateStrC(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
return format.format(date);
}
static public String getDateStrCompact(Date date) {
if (date == null)
return "";
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String str = format.format(date);
return str;
}
/**
* getDateStr get a string with format YYYY-MM-DD HH:mm:ss from a Date
* object
*
* @param date
* date
* @return String
*/
static public String getDateTimeStr(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(date);
}
static public String getDateTimeStrC(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
return format.format(date);
}
public static String getCurDateStr(String pattern) {
SimpleDateFormat format = new SimpleDateFormat(pattern);
return format.format(new Date());
}
/**
* Parses text in 'YYYY-MM-DD' format to produce a date.
*
* @param s
* the text
* @return Date
* @throws ParseException
*/
static public Date parseDate(String s) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.parse(s);
}
static public Date parseDateC(String s) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
return format.parse(s);
}
/**
* Parses text in 'YYYY-MM-DD' format to produce a date.
*
* @param s
* the text
* @return Date
* @throws ParseException
*/
static public Date parseDateTime(String s) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.parse(s);
}
static public Date parseDateTimeC(String s) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
return format.parse(s);
}
/**
* Parses text in 'HH:mm:ss' format to produce a time.
*
* @param s
* the text
* @return Date
* @throws ParseException
*/
static public Date parseTime(String s) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
return format.parse(s);
}
static public Date parseTimeC(String s) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("HH时mm分ss秒");
return format.parse(s);
}
static public int yearOfDate(Date s) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String d = format.format(s);
return Integer.parseInt(d.substring(0, 4));
}
static public int monthOfDate(Date s) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String d = format.format(s);
return Integer.parseInt(d.substring(5, 7));
}
static public int dayOfDate(Date s) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String d = format.format(s);
return Integer.parseInt(d.substring(8, 10));
}
static public String getDateTimeStr(java.sql.Date date, double time) {
int year = date.getYear() + 1900;
int month = date.getMonth() + 1;
int day = date.getDate();
String dateStr = year + "-" + month + "-" + day;
Double d = new Double(time);
String timeStr = String.valueOf(d.intValue()) + ":00:00";
return dateStr + " " + timeStr;
}
/**
* Get the total month from two date.
*
* @param sd
* the start date
* @param ed
* the end date
* @return int month form the start to end date
* @throws ParseException
*/
static public int diffDateM(Date sd, Date ed) throws ParseException {
return (ed.getYear() - sd.getYear()) * 12 + ed.getMonth()
- sd.getMonth() + 1;
}
static public int diffDateD(Date sd, Date ed) throws ParseException {
return Math.round((ed.getTime() - sd.getTime()) / 86400000) + 1;
}
static public int diffDateM(int sym, int eym) throws ParseException {
return (Math.round(eym / 100) - Math.round(sym / 100)) * 12
+ (eym % 100 - sym % 100) + 1;
}
static public java.sql.Date getNextMonthFirstDate(java.sql.Date date)
throws ParseException {
Calendar scalendar = new GregorianCalendar();
scalendar.setTime(date);
scalendar.add(Calendar.MONTH, 1);
scalendar.set(Calendar.DATE, 1);
return new java.sql.Date(scalendar.getTime().getTime());
}
static public java.sql.Date getFrontDateByDayCount(java.sql.Date date,
int dayCount) throws ParseException {
Calendar scalendar = new GregorianCalendar();
scalendar.setTime(date);
scalendar.add(Calendar.DATE, -dayCount);
return new java.sql.Date(scalendar.getTime().getTime());
}
/**
* Get first day of the month.
*
* @param year
* the year
* @param month
* the month
* @return Date first day of the month.
* @throws ParseException
*/
static public Date getFirstDay(String year, String month)
throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.parse(year + "-" + month + "-1");
}
static public Date getFirstDay(int year, int month) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.parse(year + "-" + month + "-1");
}
static public Date getLastDay(String year, String month)
throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(year + "-" + month + "-1");
Calendar scalendar = new GregorianCalendar();
scalendar.setTime(date);
scalendar.add(Calendar.MONTH, 1);
scalendar.add(Calendar.DATE, -1);
date = scalendar.getTime();
return date;
}
static public Date getLastDay(int year, int month) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(year + "-" + month + "-1");
Calendar scalendar = new GregorianCalendar();
scalendar.setTime(date);
scalendar.add(Calendar.MONTH, 1);
scalendar.add(Calendar.DATE, -1);
date = scalendar.getTime();
return date;
}
/**
* getToday get todat string with format YYYY-MM-DD from a Date object
*
* @param date
* date
* @return String
*/
static public String getTodayStr() throws ParseException {
Calendar calendar = Calendar.getInstance();
return getDateStr(calendar.getTime());
}
static public Date getToday() throws ParseException {
return new Date(System.currentTimeMillis());
}
static public String getTodayAndTime() {
return new Timestamp(System.currentTimeMillis()).toString();
}
static public String getTodayC() throws ParseException {
Calendar calendar = Calendar.getInstance();
return getDateStrC(calendar.getTime());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -