자바 날짜 조작 방법
8552 단어 자바
public class BusinessDate {
public BusinessDate() {
}
/**
* , 2009-02-11
* @return
*/
public static String getToday() {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}
/**
* , 2009-02-11 23:9:21
* @return
*/
public static String getTodaytime() {
Calendar cl = new GregorianCalendar();
return getToday() + " " + cl.get(Calendar.HOUR_OF_DAY) + ":" + cl.get(Calendar.MINUTE) + ":" + cl.get(Calendar.SECOND) + " ";
}
/**
* , 23:12:20
* @return
*/
public static String getTime() {
Calendar cl = new GregorianCalendar();
return cl.get(Calendar.HOUR_OF_DAY) + ":" + cl.get(Calendar.MINUTE) + ":" + cl.get(Calendar.SECOND) + " ";
}
/**
*
* @return
*/
public static int getHour() {
Calendar cl = new GregorianCalendar();
return cl.get(Calendar.HOUR_OF_DAY);
}
/**
* 20090211
* @return
*/
public static String getNoFormatToday() {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}
/**
* 231611
* @return
*/
public static String getNoFormatTime() {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("HHmmss");
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}
/**
*
* @return
*/
public static String getYear() {
return BusinessDate.getNoFormatToday().substring(0, 4);
}
/**
*
* @return
*/
public static String getMonth() {
return BusinessDate.getNoFormatToday().substring(4, 6);
}
/**
*
* @return
*/
public static String getDay() {
return BusinessDate.getNoFormatToday().substring(6,8 ) ;
}
/**
* 2009-02-10
* @return
*/
public static String getYesterday() {
String strYesterday = "";
Calendar cale = null;
cale = new GregorianCalendar();
cale.add(Calendar.DATE, -1);
strYesterday = BusinessDate.getStrByCalendar(cale);
return strYesterday;
}
public static String getStrByCalendar(Calendar cale) {
return (new java.text.SimpleDateFormat("yyyy-MM-dd")).format(cale.getTime());
}
/**
* , "2009-02-11" 2009 2 11
* @param sDate
* @return
*/
public static String getChnDateString(String sDate) {
if (sDate == null) {
return null;
}
sDate = sDate.trim();
if (sDate.length() == 7) {
sDate += "-01";
}
StringTokenizer st = new StringTokenizer(sDate, "-");
int year = 2100;
int month = 0;
int day = 1;
try {
year = Integer.parseInt(st.nextToken());
month = Integer.parseInt(st.nextToken()) - 1;
day = Integer.parseInt(st.nextToken());
} catch (Exception e) {
e.printStackTrace();
}
Calendar cl = new GregorianCalendar(year, month, day);
return cl.get(Calendar.YEAR) + " " + (cl.get(Calendar.MONTH) + 1) + " " + cl.get(Calendar.DATE) + " ";
}
/**
*
* @param year
* @param month
* @return
*/
public static String getMaxDayOfMonth(int year, int month) {
Calendar cal = new GregorianCalendar(year, month - 1, 1);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(cal.getTime());
}
/**
*
* @param year
* @param month
* @return
*/
public static String getMinDayOfMonth(int year, int month) {
Calendar cal = new GregorianCalendar(year, month - 1, 1);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(cal.getTime());
}
/**
* , 2006 11 28
* @return
*/
public static String getChineseToDay() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd E", Locale.CHINESE);
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}
/**
* , 2006 11 28 05:06
* @return
*/
public static String getChineseToDayTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd E a", Locale.CHINESE);
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}
/**
* ,
* @param sDate
* @return
*/
public static Calendar getDate(String sDate) {
if (sDate == null) {
return null;
}
sDate = sDate.trim();
if (sDate.length() == 7) {
sDate += "-01";
}
StringTokenizer st = new StringTokenizer(sDate, "-");
int year = 2100;
int month = 0;
int day = 1;
try {
year = Integer.parseInt(st.nextToken());
month = Integer.parseInt(st.nextToken()) - 1;
day = Integer.parseInt(st.nextToken());
} catch (Exception e) {
e.printStackTrace();
}
return new GregorianCalendar(year, month, day);
}
/**
*
* @param sDate
* @return
*/
public static String getDateString(Calendar sDate) {
if (sDate == null) {
return "";
}
return (new java.text.SimpleDateFormat("yyyy-MM-dd")).format(sDate.getTime());
}
/**
* @param sDate
* @return
*/
public static String getYearMonth(Calendar sDate) {
if (sDate == null) {
return "";
}
return (new java.text.SimpleDateFormat("yyyy-MM")).format(sDate.getTime());
}
/** , (yyyy-mm-dd)
* cale1 cale2, 1
* cale1 cale2, -1
* , 0
* , -2
* @param cale1
* @param cale2
* @return
*/
public static int compareCalendar(String cale1, String cale2) {
Calendar calendar1 = getDate(cale1);
Calendar calendar2 = getDate(cale2);
if (calendar1 == null || calendar2 == null) {
return -2;
}
return calendar1.compareTo(calendar2);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.