📄 dater.java
字号:
package com.web.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @author Administrator
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class Dater {
private static String CurrentTime;
private static String CurrentDate;
/**
* Returns the currentTime.
* @return String
*/
public static String getCurrentTime() {
Date NowDate = new Date();
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
CurrentTime = formatter.format(NowDate);
return CurrentTime;
}
/**
* Returns the currentDate.
* @return String
*/
public static String getCurrentDate() {
Date NowDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
CurrentDate = formatter.format(NowDate);
return CurrentDate;
}
/**
* Returns the Compare Result
* if equals return "0".
* if before return "1".
* if after return "2".
* if error return "4".
* @return String
*/
public static String CompareDate(String SDate1, String SDate2) {
Date DDate1, DDate2;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
DDate1 = formatter.parse(SDate1);
DDate2 = formatter.parse(SDate2);
if (DDate1.equals(DDate2)) {
return "0";
} else if (DDate1.before(DDate2)) {
return "1";
} else {
return "2";
}
} catch (ParseException e) {
e.printStackTrace();
return "4";
}
}
public static String getCurrentYear() {
java.util.Date NowDate = new java.util.Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
return formatter.format(NowDate);
}
public static String getCurrentMonth() {
java.util.Date NowDate = new java.util.Date();
SimpleDateFormat formatter = new SimpleDateFormat("MM");
return formatter.format(NowDate);
}
public static String getCurrentDay() {
java.util.Date NowDate = new java.util.Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd");
return formatter.format(NowDate);
}
public static String getCurrentHour() {
java.util.Date NowDate = new java.util.Date();
SimpleDateFormat formatter = new SimpleDateFormat("HH");
return formatter.format(NowDate);
}
public static String getYear(String yyyy_MM_dd) {
return yyyy_MM_dd.substring(0, 4);
}
public static String getMonth(String yyyy_MM_dd) {
return yyyy_MM_dd.substring(5, 7);
}
public static String getDay(String yyyy_MM_dd) {
return yyyy_MM_dd.substring(8, 10);
}
public static String getyyyyMMddDate(String yyyy, String MM, String dd) {
String result = "";
result = yyyy + "-";
if (MM.length() == 1) {
result = result + "0" + MM;
} else {
result = result + MM;
}
result = result + "-";
if (dd.length() == 1) {
result = result + "0" + dd;
} else {
result = result + dd;
}
return result;
}
public static String dateAdd(String time1, int strTo) {
try {
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = ft.parse(time1);
Calendar strDate = Calendar.getInstance(); //java.util包
strDate.setTime(date1);
strDate.add(strDate.DATE, strTo); //日期减 如果不够减会将月变动
//生成 (年-月-日) 字符串
String meStrDate =
strDate.get(strDate.YEAR)
+ "-"
+ String.valueOf(strDate.get(strDate.MONTH) + 1)
+ "-"
+ strDate.get(strDate.DATE);
return meStrDate;
} catch (Exception e) {
return time1;
}
}
public static String getOnlyDate(String yyyy_MM_dd_HH_mm_ss) {
return yyyy_MM_dd_HH_mm_ss.substring(0, 10);
}
public static void main(String args[]) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -