📄 dateutil.java
字号:
static public int getThisYearMonth() throws ParseException {
Date today = Calendar.getInstance().getTime();
return (today.getYear() + 1900) * 100 + today.getMonth() + 1;
}
static public int getYearMonth(Date date) throws ParseException {
return (date.getYear() + 1900) * 100 + date.getMonth() + 1;
}
// 获取相隔月数
static public long getDistinceMonth(String beforedate, String afterdate)
throws ParseException {
SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
long monthCount = 0;
try {
java.util.Date d1 = d.parse(beforedate);
java.util.Date d2 = d.parse(afterdate);
monthCount = (d2.getYear() - d1.getYear()) * 12 + d2.getMonth()
- d1.getMonth();
// dayCount = (d2.getTime()-d1.getTime())/(30*24*60*60*1000);
} catch (ParseException e) {
System.out.println("Date parse error!");
// throw e;
}
return monthCount;
}
// 获取相隔天数
static public long getDistinceDay(String beforedate, String afterdate)
throws ParseException {
SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
long dayCount = 0;
try {
java.util.Date d1 = d.parse(beforedate);
java.util.Date d2 = d.parse(afterdate);
dayCount = (d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000);
} catch (ParseException e) {
System.out.println("Date parse error!");
// throw e;
}
return dayCount;
}
// 获取相隔天数
static public long getDistinceDay(Date beforedate, Date afterdate)
throws ParseException {
long dayCount = 0;
try {
dayCount = (afterdate.getTime() - beforedate.getTime())
/ (24 * 60 * 60 * 1000);
} catch (Exception e) {
// System.out.println("Date parse error!");
// // throw e;
}
return dayCount;
}
static public long getDistinceDay(java.sql.Date beforedate,
java.sql.Date afterdate) throws ParseException {
long dayCount = 0;
try {
dayCount = (afterdate.getTime() - beforedate.getTime())
/ (24 * 60 * 60 * 1000);
} catch (Exception e) {
// System.out.println("Date parse error!");
// // throw e;
}
return dayCount;
}
// 获取相隔天数
static public long getDistinceDay(String beforedate) throws ParseException {
return getDistinceDay(beforedate, getTodayStr());
}
// 获取相隔时间数
static public long getDistinceTime(String beforeDateTime,
String afterDateTime) throws ParseException {
SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
long timeCount = 0;
try {
java.util.Date d1 = d.parse(beforeDateTime);
java.util.Date d2 = d.parse(afterDateTime);
timeCount = (d2.getTime() - d1.getTime()) / (60 * 60 * 1000);
} catch (ParseException e) {
System.out.println("Date parse error!");
throw e;
}
return timeCount;
}
// 获取相隔时间数
static public long getDistinceTime(String beforeDateTime)
throws ParseException {
return getDistinceTime(beforeDateTime, new Timestamp(System
.currentTimeMillis()).toLocaleString());
}
// 获取相隔分钟数
static public long getDistinceMinute(String beforeDateTime,
String afterDateTime) throws ParseException {
SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long timeCount = 0;
try {
java.util.Date d1 = d.parse(beforeDateTime);
java.util.Date d2 = d.parse(afterDateTime);
timeCount = (d2.getTime() - d1.getTime()) / (60 * 1000);
} catch (ParseException e) {
System.out.println("Date parse error!");
throw e;
}
return timeCount;
}
// 获取相隔分钟数
static public long getDistinceMinute(String afterDateTime)
throws ParseException {
return getDistinceMinute(new Timestamp(System.currentTimeMillis())
.toLocaleString(), afterDateTime);
}
// 判断是否超出指定相隔时间范围内
static public boolean isOvertime(String beforeDateTime, String timeCount) {
boolean exceed = false;
try {
long count1 = Long.parseLong(timeCount);
long count2 = getDistinceTime(beforeDateTime);
if (count1 < count2) {
exceed = true;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return exceed;
}
static public String getTimestamStr(Timestamp timestamp) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(timestamp);
}
static public String getTimeStr(Time time) {
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
return format.format(time);
}
// 判断后者时间是否为前者时间前
static public boolean isBeforeCheckDate(String checkdate,
java.util.Date auditDate) throws ParseException {
java.util.Date cd;
try {
cd = new java.util.Date(parseDate(checkdate).getTime());
} catch (ParseException ex) {
System.out.println(ex);
return false;
}
return isBeforeCheckDate(cd, auditDate);
}
static private boolean isBeforeCheckDate(java.util.Date checkdate,
java.util.Date auditDate) throws ParseException {
return auditDate.before(checkdate);
}
static public java.sql.Date getNextMonthDate(java.sql.Date date)
throws ParseException {
Calendar scalendar = new GregorianCalendar();
scalendar.setTime(date);
scalendar.add(Calendar.MONTH, 1);
return new java.sql.Date(scalendar.getTime().getTime());
}
static public String format(Date date, String formatText) throws Exception {
SimpleDateFormat format = new SimpleDateFormat(formatText);
return format.format(date);
}
static public int getDaysOfMonth(Date startdate, Date enddate, String month)
throws Exception {
int startmonth = startdate.getMonth() + 1;
int endmonth = enddate.getMonth() + 1;
int m = Integer.parseInt(month);
int day = getLastDay(String.valueOf(startdate.getYear()), month)
.getDate();
if ((startmonth < m) && (m < endmonth)) {
return day;
} else if (m == startmonth) {
return day - startdate.getDate() + 1;
} else if (m == endmonth) {
return enddate.getDate();
}
return 0;
}
public static void main(String[] args) {
try {
// getDaysOfMonth(new Date(),new Date(),"02");
// Double s=Double.valueOf("123.6");
// System.out.print(s.doubleValue()/2);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
int days = getDaysOfMonth(format.parse("2007-6-6"), format
.parse("2007-7-7"), "07");
System.out.print("days-->" + days);
// System.out.print(getDistinceDay(new
// Date(),format.parse("2007-7-7")));
// java.text.SimpleDateFormat format = new
// java.text.SimpleDateFormat(
// "yyyy-MM-dd");
// System.out.println("date->" +
// format.format(new Date(getLastDay("2004", "10").
// getTime())));
// java.sql.Date date = new
// java.sql.Date(System.currentTimeMillis());
// System.out.println(getFrontDateByDayCount(date, 80));
// Date date = getLastDay("2004","12");
// System.out.println(getThisYearMonth());
// System.out.println(getTodayAndTime());
// String s = getDateTimeStr(new
// java.sql.Date(System.currentTimeMillis()), 12.0d);
// System.out.println(s);
// System.out.println(getDistinceMinute(s, "2005-7-20 12:45:58"));
// System.out.println(isOvertime("2005-3-15 2:55:00", "26"));
// String s = "2005-3-23 14:35:58.177";
// Timestamp t = Timestamp.valueOf(s);
// System.out.println(t.toString());
// String s = new Timestamp(System.currentTimeMillis()).toString();
// System.out.println(s);
// System.out.println(getToday());
// Timestamp t = Timestamp.valueOf(s);
// System.out.println(t.toString());
// System.out.println(getDistinceTime(s));
// System.out.println(new Timestamp(0));
// String s = getDateTimeStr(new
// java.sql.Date(System.currentTimeMillis()), 12.0d);
// System.out.println(s);
// System.out.println(getDistinceMonth(s, "2006-11-20 12:45:58"));
// String s1 = "2006-2-1";
// String s2 = "2006-2-28";
// float f1 = 5;
// float f2 = 3;
// System.out.println(f1/f2);
// NumberFormat format = new DecimalFormat("0.00");
// String result = format.format(-1234561111111.1);
// System.out.println("number->"+result);
} catch (Exception ex) {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -