共通コード:java日付処理
個人整理のjavaにおける日付処理が適用されるプログラムについて.
/**
* Copyright ? 2010 mywhile Co.Ltd.
* All right reserved.
*
* :yshlin
* @author yshlin
* E-mail:[email protected]
*/
package com.mywhile.global.tools;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
public class GlobalDateMethod {
/*********************************bean***************************************/
private String temp;
private String info;
private ArrayList<String> week;
private static long DAY = 24L * 60L * 60L * 1000L;
/*********************************set/get***************************************/
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public ArrayList<String> getWeek() {
return week;
}
public void setWeek(ArrayList<String> week) {
this.week = week;
}
/**********************************static method****************************************/
/**
* Date
* @param strDate
* @return
* : :2010-12-17
* :Fri Dec 17 00:00:00 CST 2010
*/
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
*
* @param strdate
* @return
* : :2010-12-17
* :5
* 2010 12 17
*/
public static String getWeekDayIntNumberForStrDate(String strdate) {
Date date = strToDate(strdate);
Calendar c = Calendar.getInstance();
c.setTime(date);
//hour , 1~7
//Calendar.DAY_OF_WEEK :1= 7= ,
int week=c.get(Calendar.DAY_OF_WEEK)-1;
return week+"";
}
/**
* start > end
* @param start
* @param end
* @return
* :
* "2010-12-01" before "2010-11-1" = false;
* "2010-12-01" before "2010-12-01" = false;
* "2010-11-01" before "2010-12-1" = true;
*/
public static boolean checkStartDateBeforeEndDate(String start, String end) {
return strToDate(start).before(strToDate(end));
}
/**
* start < end
* @param start
* @param end
* @return
* :
* "2010-12-01" after "2010-11-1" = true;
* "2010-12-01" after "2010-12-01" = false;
* "2010-11-01" after "2010-12-1" = false;
*/
public static boolean checkStartDateAfterEndDate(String start, String end) {
return strToDate(start).after(strToDate(end));
}
/**
* ,sdate ,delay
* @param nowdate YYYY-MM-DD
* @param delay
* @return
*/
public static String getNextDay(String sdate, int delay) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String mdate = "";
Date d = strToDate(sdate);
long myTime = (d.getTime() / 1000) + delay * 24 * 60 * 60;
d.setTime(myTime * 1000);
mdate = format.format(d);
return mdate;
} catch (Exception e) {
return "";
}
}
/**
*
* @return yyyy-MM-dd
*/
public static String getStringDateShort() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* yyyy-MM-dd
* @param date YYYY-MM-DD
* @return
*/
public static String dateToStr(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(date);
return dateString;
}
/**
*
* @param sdate1 YYYY-MM-DD
* @param sdate2 YYYY-MM-DD
* @return
* ,
*/
public static String getTwoDay(String sdate1, String sdate2) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
long day = 0;
try {
Date date = myFormatter.parse(sdate1);
Date mydate = myFormatter.parse(sdate2);
day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
} catch (Exception e) {
return "";
}
return day + "";
}
/**
*
* @return
*/
public static String[] getNowDayForWeek(){
String now = getStringDateShort();
String num = getWeekDayIntNumberForStrDate(now);
String temp[] = new String[7];
if(num.equals("5")){
temp[0] = GlobalDateMethod.getNextDay(now, -4);
temp[1] = GlobalDateMethod.getNextDay(now, -3);
temp[2] = GlobalDateMethod.getNextDay(now, -2);
temp[3] = GlobalDateMethod.getNextDay(now, -1);
temp[4] = now;
temp[5] = GlobalDateMethod.getNextDay(now, 1);
temp[6] = GlobalDateMethod.getNextDay(now, 2);
}else if(num.equals("4")){
temp[0] = GlobalDateMethod.getNextDay(now, -3);
temp[1] = GlobalDateMethod.getNextDay(now, -2);
temp[2] = GlobalDateMethod.getNextDay(now, -1);
temp[3] = now;
temp[4] = GlobalDateMethod.getNextDay(now, 1);
temp[5] = GlobalDateMethod.getNextDay(now, 2);
temp[6] = GlobalDateMethod.getNextDay(now, 3);
}else if(num.equals("3")){
temp[0] = GlobalDateMethod.getNextDay(now, -2);
temp[1] = GlobalDateMethod.getNextDay(now, -1);
temp[2] = now;
temp[3] = GlobalDateMethod.getNextDay(now, 1);
temp[4] = GlobalDateMethod.getNextDay(now, 2);
temp[5] = GlobalDateMethod.getNextDay(now, 3);
temp[6] = GlobalDateMethod.getNextDay(now, 4);
}else if(num.equals("2")){
temp[0] = GlobalDateMethod.getNextDay(now, -1);
temp[1] = now;
temp[2] = GlobalDateMethod.getNextDay(now, 1);
temp[3] = GlobalDateMethod.getNextDay(now, 2);
temp[4] = GlobalDateMethod.getNextDay(now, 3);
temp[5] = GlobalDateMethod.getNextDay(now, 4);
temp[6] = GlobalDateMethod.getNextDay(now, 5);
}else if(num.equals("1")){
temp[0] = now;
temp[1] = GlobalDateMethod.getNextDay(now, 1);
temp[2] = GlobalDateMethod.getNextDay(now, 2);
temp[3] = GlobalDateMethod.getNextDay(now, 3);
temp[4] = GlobalDateMethod.getNextDay(now, 4);
temp[5] = GlobalDateMethod.getNextDay(now, 5);
temp[6] = GlobalDateMethod.getNextDay(now, 6);
}else if(num.equals("0")){
temp[0] = GlobalDateMethod.getNextDay(now, -6);
temp[1] = GlobalDateMethod.getNextDay(now, -5);
temp[2] = GlobalDateMethod.getNextDay(now, -4);
temp[3] = GlobalDateMethod.getNextDay(now, -3);
temp[4] = GlobalDateMethod.getNextDay(now, -2);
temp[5] = GlobalDateMethod.getNextDay(now, -1);
temp[6] = now;
}else{
temp[0] = GlobalDateMethod.getNextDay(now, -5);
temp[1] = GlobalDateMethod.getNextDay(now, -4);
temp[2] = GlobalDateMethod.getNextDay(now, -3);
temp[3] = GlobalDateMethod.getNextDay(now, -2);
temp[4] = GlobalDateMethod.getNextDay(now, -1);
temp[5] = now;
temp[6] = GlobalDateMethod.getNextDay(now, 1);
}
return temp;
}
/**
*
* @param date YYYY-MM-DD
* @return
* : :2010-01-15 :2010-01-11,2010-01-17
*/
public static String dayOfWeekStartEnd(String date) {
String reD = "";
Date today = GlobalDateMethod.strToDate(date);;
Calendar cal = Calendar.getInstance();
cal.setTime(today);
int between = cal.get(Calendar.DAY_OF_WEEK) - 1;
int subMonday = 0;
if (between >= 1 && between <= 6) {
subMonday = 1 - between;
} else if (between == 0) {
subMonday = -6;
}
cal.add(Calendar.DAY_OF_MONTH, subMonday);
reD += new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
cal.add(Calendar.DAY_OF_MONTH, 6);
reD += ("," + new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));
return reD;
}
/**
* ,
* @param date YYYY-MM-DD
* @return
*/
public static String dayOfWeekStart(String date) {
String reD = "";
Date today = GlobalDateMethod.strToDate(date);;
Calendar cal = Calendar.getInstance();
cal.setTime(today);
int between = cal.get(Calendar.DAY_OF_WEEK) - 1;
int subMonday = 0;
if (between >= 1 && between <= 6) {
subMonday = 1 - between;
} else if (between == 0) {
subMonday = -6;
}
cal.add(Calendar.DAY_OF_MONTH, subMonday);
reD = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
cal.add(Calendar.DAY_OF_MONTH, 6);
//reD += ("," + new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));
return reD;
}
/**
* , ,
* @param date YYYY-MM-DD
* @return
*/
public static String dayOfWeekEnd(String date) {
String reD = "";
Date today = GlobalDateMethod.strToDate(date);;
Calendar cal = Calendar.getInstance();
cal.setTime(today);
int between = cal.get(Calendar.DAY_OF_WEEK) - 1;
int subMonday = 0;
if (between >= 1 && between <= 6) {
subMonday = 1 - between;
} else if (between == 0) {
subMonday = -6;
}
cal.add(Calendar.DAY_OF_MONTH, subMonday);
reD = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
cal.add(Calendar.DAY_OF_MONTH, 6);
reD = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
return reD;
}
/**
*
* @param date1 YYYY-MM-DD
* @param date2 YYYY-MM-DD
* @return
*/
public static long getDaysLong(String date1, String date2) {
if (date1 == null || date1.equals(""))
return 0;
if (date2 == null || date2.equals(""))
return 0;
//
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
Date mydate = null;
try {
date = myFormatter.parse(date1);
mydate = myFormatter.parse(date2);
} catch (Exception e) {
}
long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
return day;
}
/**
* ;
* @param date1 YYYY-MM-DD
* @param date2 YYYY-MM-DD
* @return
*/
public static int getDaysInt(String date1, String date2) {
if (date1 == null || date1.equals(""))
return 0;
if (date2 == null || date2.equals(""))
return 0;
//
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
Date mydate = null;
try {
date = myFormatter.parse(date1);
mydate = myFormatter.parse(date2);
} catch (Exception e) {
}
int day = (int)((date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000));
return day;
}
/**
* , , ,
* @param start YYYY-MM-DD
* @param end YYYY-MM-DD
* @return
*/
public static String[] getWeekOfStartEndDate(String start, String end){
String sdate = dayOfWeekStart(start);
String edate = dayOfWeekEnd(end);
int days = getDaysInt(edate, sdate);
days = days / 7 + ((days % 7) > 0 ? 1 : 0);
String reS[] = new String[days];
for(int i=0;i<days;i++){
String d = dayOfWeekStartEnd(sdate);
int ts = getDaysInt(edate, sdate);
if(ts < 0){
return reS;
}else{
reS[i] = d;
}
sdate = getNextDay(sdate, 7);
}
return reS;
}
}