📄 dateutil.java
字号:
package org.pontifex.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Created by IntelliJ IDEA.
* User: Songzou
* Date: 2007-4-8
* Time: 11:27:55
*
*
* 日期操作类
*/
public class DateUtil {
/**
* 判断字符串是否是有效的日期,
* 格式"yyyy-MM-dd,yyyy-MM-d,yyyy-M-dd,yyyy-M-d
* "yyyy/MM/dd,yyyy/MM/d,yyyy/M/dd,yyyy/M/d"
* "yyyyMMdd"
*
* @param date 日期字符串
* @return 是则返回true,否则返回false
*/
public static boolean isValidDate(String date) {
if ((date == null) || (date.length() < 8)) {
return false;
}
try {
boolean result = false;
SimpleDateFormat formatter;
char dateSpace = date.charAt(4);
String format[];
if ((dateSpace == '-') || (dateSpace == '/')) {
format = new String[4];
String strDateSpace = Character.toString(dateSpace);
format[0] = "yyyy" + strDateSpace + "MM" + strDateSpace + "dd";
format[1] = "yyyy" + strDateSpace + "MM" + strDateSpace + "d";
format[2] = "yyyy" + strDateSpace + "M" + strDateSpace + "dd";
format[3] = "yyyy" + strDateSpace + "M" + strDateSpace + "d";
}
else {
format = new String[1];
format[0] = "yyyyMMdd";
}
for (int i = 0; i < format.length; i++) {
String aFormat = format[i];
formatter = new SimpleDateFormat(aFormat);
formatter.setLenient(false);
String tmp = formatter.format(formatter.parse(date));
if (date.equals(tmp)) {
result = true;
break;
}
}
return result;
}
catch (ParseException e) {
return false;
}
}
/**
* 判断字符串是否是有效的日期,格式"yyyy-MM-dd HH:mm:ss"
*
* @param date 日期字符串
* @return 是则返回true,否则返回false
*/
public static boolean isValidTime(String date) {
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.setLenient(false);
formatter.parse(date);
return true;
}
catch (ParseException e) {
return false;
}
}
/**
* 转换字符串为日期,格式"yyyy-MM-dd"
*
* @param date 日期字符串
* @return 返回格式化的日期
*/
public static Date strToDate(String date) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
formatter.setLenient(false);
return formatter.parse(date);
}
/**
* 转换字符串为日期,格式"yyyy-MM-dd HH:mm:ss"
*
* @param date 日期字符串
* @return 返回格式化的日期
*/
public static Date strToTime(String date) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.setLenient(false);
return formatter.parse(date);
}
/**
* 转换日期为字符串,格式"yyyy-MM-dd"
*
* @param date 日期
* @return 返回格式化的日期字符串
*/
public static String dateToStr(Date date) {
if (date == null) return null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(date);
}
/**
* 转换日期为字符串,格式"yyyy-MM-dd HH:mm:ss"
*
* @param date 日期
* @return 返回格式化的日期字符串
*/
public static String timeToStr(Date date) {
if (date == null) return null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(date);
}
/**
* 取得现在的日期,格式"yyyy-MM-dd HH:mm:ss"
*
* @return 返回格式化的日期字符串
*/
public static String getNow() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date Now = new Date();
return formatter.format(Now);
}
/**
* 取得现在的日期,格式"yyyy-MM-dd"
*
* @return 返回格式化的日期字符串
*/
public static String getDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date Now = new Date();
return formatter.format(Now);
}
/**
* 取得现在的时间,格式"HH:mm:ss"
*
* @return 返回格式化的时间字符串
*/
public static String getTime() {
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
Date Now = new Date();
return formatter.format(Now);
}
/**
* 取得年份,格式"yyyy"
*
* @return 返回当前年份
*/
public static int getYear() {
Date Now = new Date();
return getYear(Now);
}
/**
* 取得日期的年份,格式"yyyy"
*
* @param date 日期
* @return 日期的年份
*/
public static int getYear(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
return Integer.parseInt(formatter.format(date));
}
/**
* 取得月份
*
* @return 返回当前月份
*/
public static int getMonth() {
Date Now = new Date();
return getMonth(Now);
}
/**
* 取得日期的月份
*
* @param date 日期
* @return 日期的月份
*/
public static int getMonth(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("M");
return Integer.parseInt(formatter.format(date));
}
/**
* 取得今天的日期数
*
* @return 返回今天的日期数
*/
public static int getDay() {
Date Now = new Date();
return getDay(Now);
}
/**
* 取得日期的天数
*
* @param date 日期
* @return 日期的天数
*/
public static int getDay(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("d");
return Integer.parseInt(formatter.format(date));
}
/**
* 获得与某日期相隔几天的日期
*
* @param date 指定的日期
* @param addCount 相隔的天数
* @return 返回的日期
*/
public static Date addDay(Date date, int addCount) throws ParseException {
//Calendar cal = new GregorianCalendar();
//最好用Calendar.getInstance();
//因为有的地方,不是使用GregorianCalendar的。
Calendar calendar = Calendar.getInstance();
calendar.setTime(strToDate(dateToStr(date)));
calendar.add(Calendar.DATE, addCount);
return calendar.getTime();
}
/**
* 获得与某日期相隔几月的日期
*
* @param date 指定的日期
* @param addCount 相隔的月数
* @return 返回的日期
*/
public static Date addMonth(Date date, int addCount) throws ParseException {
Calendar calendar = Calendar.getInstance();
calendar.setTime(strToDate(dateToStr(date)));
calendar.add(Calendar.MONTH, addCount);
return calendar.getTime();
}
//**************************************************
//public static long getDistDay(Date earlier, Date later)
/*
public static long getDistDay()
{
Calendar calendar = Calendar.getInstance();
//calendar.setTime(strToDate(dateToStr(earlier)));
long i = calendar.getTimeInMillis();
System.out.println(calendar.getTime());
System.out.println(calendar.getTimeInMillis());
System.out.println(calendar.getTime());
System.out.println(calendar.getTimeInMillis());
return i;
}
public static void main(String[] args)
{
//System.out.println();
getDistDay();
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -