日付フレームワークの時間操作
18463 ワード
火曜日の午前:静かに何冊かの本をダウンロードして、勝手に読んでみました.「Redis実戦」Redisの利点:クエリーの速度が速い;従来のデータベースの何倍も高速です.ビッグデータ量へのアクセスに適しています.格納方式はキー値対格納であり、テーブル間には関連関係がないため、NoSQLとも呼ばれる.値の格納タイプは5種類あり、普通の文字列、リスト、集合、分散リスト、秩序化集合である.その後、本の中で主によく見られるシステム機能を通じてRedisを紹介したが、私は詳しく見ていないので、簡単に整理した.クッキーキャッシュ;ショッピングカートサーバ構成;ログ;自動補完;分散ロック;ファイルの配布;索引;最適化;拡張;Luaスクリプト;クイックインストールガイド;後で詳しく見る時間があります.
その後、会社のフレームワークに実用的なツールクラスがたくさんあることを思い出して、よく見てください.時間関連ツールクラス:TimeHelper import java.text.; import java.util.;
その後、会社のフレームワークに実用的なツールクラスがたくさんあることを思い出して、よく見てください.時間関連ツールクラス:TimeHelper import java.text.; import java.util.;
/**
* @author Wurd 2005-11-27
*
*
*/
public class TimeHelper {
//
private static String CurrentTime;
//
private static String CurrentDate;
/**
* :yyyy
*
* @return String
*/
public static String getCurrentYear() {
java.util.Date NowDate = new java.util.Date();
//
SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
return formatter.format(NowDate);
}
/**
* :MM
*
* @return String
*/
public static String getCurrentMonth() {
java.util.Date NowDate = new java.util.Date();
//
SimpleDateFormat formatter = new SimpleDateFormat("MM");
return formatter.format(NowDate);
}
/**
* :dd
*
* @return String
*/
public static String getCurrentDay() {
java.util.Date NowDate = new java.util.Date();
//
SimpleDateFormat formatter = new SimpleDateFormat("dd");
return formatter.format(NowDate);
}
/**
* , , 19 :yyyy-MM-dd HH:mm:ss
*
* @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;
}
public static String getCurrentCompactTime() {
Date NowDate = new Date();
//
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
CurrentTime = formatter.format(NowDate);
return CurrentTime;
}
@SuppressWarnings("static-access")
public static String getYesterdayCompactTime() {
Calendar cal = Calendar.getInstance();
cal.add(cal.DATE, -1);
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
CurrentTime = formatter.format(cal.getTime());
return CurrentTime;
}
@SuppressWarnings("static-access")
public static String getYesterdayCompactTimeForFileName() {
Calendar cal = Calendar.getInstance();
cal.add(cal.DATE, -1);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
CurrentTime = formatter.format(cal.getTime());
return CurrentTime;
}
/**
* , 10 :yyyy-MM-dd
*
* @return String
*/
public static String getCurrentDate() {
Date NowDate = new Date();
//
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
CurrentDate = formatter.format(NowDate);
return CurrentDate;
}
/**
* , 10 :yyyy-MM-dd
*
* @return String
*/
public static String getCurrentDate1() {
Date NowDate = new Date();
//
SimpleDateFormat formatter = new SimpleDateFormat("yyyy MM dd ");
CurrentDate = formatter.format(NowDate);
return CurrentDate;
}
/**
* , 10 :yyyy-MM-dd
*
* @return String
*/
public static String getCurrentFirstDate() {
Date NowDate = new Date();
//
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-01");
CurrentDate = formatter.format(NowDate);
return CurrentDate;
}
/**
* , 10 :yyyy-MM-dd
*
* @return String
* @throws ParseException
*/
public static String getCurrentLastDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar=null;
try {
java.util.Date date =formatter.parse(getCurrentFirstDate());
calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH,1);
calendar.add(Calendar.DAY_OF_YEAR, -1);
return formatDate(calendar.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
*
*
* @param date Date
* @return String
*/
public static String formatDate(java.util.Date date)
{
return formatDateByFormat(date,"yyyy-MM-dd");
}
/**
*
*
* @param date Date
* @param format String
* @return String
*/
public static String formatDateByFormat(java.util.Date date,String format)
{
String result = "";
if(date != null)
{
try
{
SimpleDateFormat sdf = new SimpleDateFormat(format);
result = sdf.format(date);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
return result;
}
/**
* , :currentdate : String yyyy-MM-dd add_day :
* int :yyyy-MM-dd
*/
public static String addDay(String currentdate, int add_day) {
GregorianCalendar gc = null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
int year, month, day;
try {
year = Integer.parseInt(currentdate.substring(0, 4));
month = Integer.parseInt(currentdate.substring(5, 7)) - 1;
day = Integer.parseInt(currentdate.substring(8, 10));
gc = new GregorianCalendar(year, month, day);
gc.add(GregorianCalendar.DATE, add_day);
return formatter.format(gc.getTime());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* , :currentdate : String yyyy-MM-dd add_month :
* int :yyyy-MM-dd
*/
public static String addMonth(String currentdate, int add_month) {
GregorianCalendar gc = null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
int year, month, day;
try {
year = Integer.parseInt(currentdate.substring(0, 4));
month = Integer.parseInt(currentdate.substring(5, 7)) - 1;
day = Integer.parseInt(currentdate.substring(8, 10));
gc = new GregorianCalendar(year, month, day);
gc.add(GregorianCalendar.MONTH, add_month);
return formatter.format(gc.getTime());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* endTime beforeTime , :endTime、beforeTime : String 7 yyyy-MM
*/
public static int monthDiff(String beforeTime,String endTime){
if(beforeTime==null||endTime==null){
return 0;
}
int beforeYear,endYear,beforeMonth,endMonth;
try {
beforeYear = Integer.parseInt(beforeTime.substring(0, 4));
endYear = Integer.parseInt(endTime.substring(0, 4));
beforeMonth = Integer.parseInt(beforeTime.substring(5, 7)) - 1;
endMonth = Integer.parseInt(endTime.substring(5, 7)) - 1;
return (endYear-beforeYear)*12+(endMonth-beforeMonth);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* :currentdatetime : String yyyy-MM-dd HH:mm:ss
* add_minute : int :yyyy-MM-dd HH:mm:ss
*/
public static String addMinute(String currentdatetime, int add_minute) {
GregorianCalendar gc = null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int year, month, day, hour, minute, second;
try {
year = Integer.parseInt(currentdatetime.substring(0, 4));
month = Integer.parseInt(currentdatetime.substring(5, 7))-1;
day = Integer.parseInt(currentdatetime.substring(8, 10));
hour = Integer.parseInt(currentdatetime.substring(11, 13));
minute = Integer.parseInt(currentdatetime.substring(14, 16));
second = Integer.parseInt(currentdatetime.substring(17, 19));
gc = new GregorianCalendar(year, month, day, hour, minute, second);
gc.add(GregorianCalendar.MINUTE, add_minute);
return formatter.format(gc.getTime());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* :currentdatetime : String yyyy-MM-dd HH:mm:ss
* minute : int :yyyy-MM-dd HH:mm:ss
*/
@SuppressWarnings("unused")
public static String reductionMinute(String currentdatetime, int minute) {
GregorianCalendar gc = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = format.parse(currentdatetime);
long Time=(date.getTime()/1000)-60*minute;
date.setTime(Time*1000);
return format.format(date.getTime());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* :currentdatetime : String yyyy-MM-dd HH:mm:ss
* add_second : int :yyyy-MM-dd HH:mm:ss
*/
public static String addSecond(String currentdatetime, int add_second) {
GregorianCalendar gc = null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int year, month, day, hour, minute, second;
try {
year = Integer.parseInt(currentdatetime.substring(0, 4));
month = Integer.parseInt(currentdatetime.substring(5, 7))-1;
day = Integer.parseInt(currentdatetime.substring(8, 10));
hour = Integer.parseInt(currentdatetime.substring(11, 13));
minute = Integer.parseInt(currentdatetime.substring(14, 16));
second = Integer.parseInt(currentdatetime.substring(17, 19));
gc = new GregorianCalendar(year, month, day, hour, minute, second);
gc.add(GregorianCalendar.SECOND, add_second);
return formatter.format(gc.getTime());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String addMinute1(String currentdatetime, int add_minute) {
GregorianCalendar gc = null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
int year, month, day, hour, minute, second;
try {
year = Integer.parseInt(currentdatetime.substring(0, 4));
month = Integer.parseInt(currentdatetime.substring(5, 7)) - 1;
day = Integer.parseInt(currentdatetime.substring(8, 10));
hour = Integer.parseInt(currentdatetime.substring(8, 10));
minute = Integer.parseInt(currentdatetime.substring(8, 10));
second = Integer.parseInt(currentdatetime.substring(8, 10));
gc = new GregorianCalendar(year, month, day, hour, minute, second);
gc.add(GregorianCalendar.MINUTE, add_minute);
return formatter.format(gc.getTime());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static Date parseDate(String sDate) {
SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = bartDateFormat.parse(sDate);
return date;
} catch (Exception ex) {
}
return null;
}
public static Date parseDate2(String sDate) {
SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy/MM/dd");
try {
Date date = bartDateFormat.parse(sDate);
return date;
} catch (Exception ex) {
}
return null;
}
/**
*
*
* @param sDateTime
*
* @return
*/
public static Date parseDateTime(String sDateTime) {
SimpleDateFormat bartDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
try {
Date date = bartDateFormat.parse(sDateTime);
return date;
} catch (Exception ex) {
}
return null;
}
/**
* date:yyyy-MM-dd
*
* @throws ParseException
*/
public static int getTotalDaysOfMonth(String strDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = new GregorianCalendar();
Date date = null;
try {
date = sdf.parse(strDate);
calendar.setTime(date);
} catch (Exception ex) {
ex.printStackTrace();
}
int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); //
return day;
}
public static long getDateSubDay(String startDate ,String endDate ) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
long theday = 0;
try {
calendar.setTime(sdf.parse(startDate));
long timethis = calendar.getTimeInMillis();
calendar.setTime(sdf.parse(endDate));
long timeend = calendar.getTimeInMillis();
theday = (timeend - timethis ) / (1000 * 60 * 60 * 24);
}catch(Exception e) {
e.printStackTrace();
}
return theday;
}
/**
* :
* @param sDateTime
* @param eDateTime
* @return
* @throws ParseException
*/
public static String getPlusTime(String sDateTime,String eDateTime) throws ParseException {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date ssDateTime= myFormatter.parse(sDateTime);
java.util.Date eeDateTime= myFormatter.parse(eDateTime);
long l=(eeDateTime.getTime()-ssDateTime.getTime());
long day=l/(24*60*60*1000);
long hour=(l/(60*60*1000)-day*24);
long min=((l/(60*1000))-day*24*60-hour*60);
long s=(l/1000-day*24*60*60-hour*60*60-min*60);
return ""+day+" "+hour+" "+min+" "+s+" ";
}
/**
* :
* @param sDateTime
* @param eDateTime
* @return
* @throws ParseException
*/
public static int getTime(String sDateTime,String eDateTime) throws ParseException {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date ssDateTime= myFormatter.parse(sDateTime);
java.util.Date eeDateTime= myFormatter.parse(eDateTime);
long l=(eeDateTime.getTime()-ssDateTime.getTime());
long day=l/(24*60*60*1000);
return (int)day;
}
/**
* :
* @param sDateTime
* @param eDateTime
* @return
* @throws ParseException
*/
public static long getPlusTotalTime(String sDateTime,String eDateTime) throws ParseException {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date ssDateTime= myFormatter.parse(sDateTime);
java.util.Date eeDateTime= myFormatter.parse(eDateTime);
return (eeDateTime.getTime()-ssDateTime.getTime());
}
/**
* ,
*
* @param sdate
* @return
*/
public static String getWeek(String sdate) {
//
Date date = TimeHelper.strToDate(sdate);
Calendar c = Calendar.getInstance();
c.setTime(date);
// int hour=c.get(Calendar.DAY_OF_WEEK);
// hour , 1~7
// 1= 7= ,
return new SimpleDateFormat("EEEE").format(c.getTime());
}
/**
* yyyy-MM-dd
*
* @param strDate
* @return
*/
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;
}
public static String convertToString(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
return formatter.format(date);
} catch (Exception e) {
// e.printStackTrace();
return null;
}
}
/**
*
*/
public static String getTwoDay(String sj1, String sj2) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
long day = 0;
try {
java.util.Date date = myFormatter.parse(sj1);
java.util.Date mydate = myFormatter.parse(sj2);
day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
} catch (Exception e) {
return "";
}
return day + "";
}
/**
* :
* @param sDateTime
* @param eDateTime
* @return
* @throws ParseException
*/
public static String getTimeByMillis(long now) throws ParseException {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(now);
return formatter.format(calendar.getTimeInMillis());
}
/*public static void main(String[] args) throws ParseException {
long s =1348211141000L;
System.out.println(new TimeHelper().getTimeByMillis(s));
}*/
}