在編寫代碼中,會經常用到時間Date這個類,小編整理了一些常用的時間工具類。供大家參考。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
/**
* 日期工具類
*/
public class DateUtils {
/**
* 獲取格林威治時間(1970年至今的秒數)
*/
public static long getGMTime1() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Etc/Greenwich"));
String format = sdf.format(new Date());
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date gmDate = null;
try {
gmDate = sdf1.parse(format);
} catch (ParseException e) {
e.printStackTrace();
}
return gmDate.getTime() / 1000;
}
/**
* 獲取格林威治時間 即1970年至今的秒數
*/
public static long getGMTime2() {
int round = (int) (System.currentTimeMillis() / 1000);
return round;
}
/**
* 獲取時間HH:mm:ss
*
* @return
*/
public static String getCurrentTime() {
String time = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(new Date());
//"\\s"以空格截斷
String[] split = date.split("\\s");
if (split.length > 1) {
time = split[1];
}
return time;
}
/**
* 獲取當前時間的年月日時分秒
* @return
*/
public static String current() {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
return year + "年" + month + "月" + day + "日" + hour + "時" + minute + "分" + second + "秒";
}
/**
* 得到昨天的日期
*
* @return
*/
public static String getYesterdayDate() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String yestoday = sdf.format(calendar.getTime());
return yestoday;
}
/**
* 得到今天的日期
*
* @return
*/
public static String getTodayDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date());
return date;
}
/**
* 得到明天的日期
*
* @return
*/
public static String getTomorrowDate() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String tomorrow = sdf.format(calendar.getTime());
return tomorrow;
}
/**
* 時間轉化爲時間格式
*
* @param timeStamp
* @return
*/
public static String timeStampToStr(long timeStamp) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(timeStamp * 1000);
return date;
}
/**
* 時間轉化爲時間格式
*
* @param timeStamp
* @return
*/
public static String timeStampToStr1(long timeStamp) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String date = sdf.format(timeStamp * 1000);
return date;
}
/**
* 時間轉化爲時間(幾點)
*
* @param time
* @return
*/
public static String timeStampToTime(long time) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String date = sdf.format(time * 1000);
return date;
}
/**
* 將日期格式轉化爲時間(秒數)
*
* @param time
* @return
*/
public static long getStringToDate(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = new Date();
try {
date = sdf.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date.getTime() / 1000;
}
/**
* 將日期格式轉化爲時間(秒數)
*
* @param time
* @return
*/
public static long getString2Date(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
try {
date = sdf.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date.getTime() / 1000;
}
/**
* 判斷是否大於當前時間
*
* @param time
* @return
*/
public static boolean judgeCurrTime(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = new Date();
try {
date = sdf.parse(time);
long t = date.getTime();
long round = System.currentTimeMillis();
if (t - round > 0) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 判斷是否大於當前時間
*
* @param time
* @return
*/
public static boolean judgeCurrTime(long time) {
long round = System.currentTimeMillis();
if (time - round > 0) {
return true;
} else {
return false;
}
}
/**
* 比較後面的時間是否大於前面的時間
*
* @param
* @return
*/
public static boolean judgeTime2Time(String time1, String time2) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
//轉化爲時間
Date date1 = sdf.parse(time1);
Date date2 = sdf.parse(time2);
//獲取秒數作比較
long l1 = date1.getTime() / 1000;
long l2 = date2.getTime() / 1000;
if (l2 - l1 > 0) {
return true;
} else {
return false;
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
/**
* 得到日期 yyyy-MM-dd
*
* @param time
* @return
*/
public static String formatDate(long time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(time * 1000);
return date;
}
/**
* 得到時間 HH:mm:ss
*
* @param timeStamp
* @return
*/
public static String getTime(long timeStamp) {
String time = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(timeStamp * 1000);
String[] split = date.split("\\s");
if (split.length > 1) {
time = split[1];
}
return time;
}
/**
* 將一個時間轉換成提示性時間字符串,(多少分鐘)
*
* @param timeStamp
* @return
*/
public static String timeStampToFormat(long timeStamp) {
long curTime = System.currentTimeMillis() / (long) 1000;
long time = curTime - timeStamp;
return time / 60 + "";
}
/**
* 獲得當前時間差
*
* @param timeStamp
* @return
*/
public static int nowCurrentTime(long timeStamp) {
long curTime = System.currentTimeMillis() / (long) 1000;
long time = timeStamp - curTime;
return (int) time;
}
/**
* 獲取當前的時 -->flag==true
* 獲取當前的分 -->flag==false
*
* @return
*/
public static String nowCurrentPoint(boolean flag) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String date = sdf.format(System.currentTimeMillis());
String[] split = date.split(":");
String hour = null;
String minute = null;
if (flag) {
if (split.length > 1) {
hour = split[0];
return hour;
}
} else {
if (split.length > 1) {
minute = split[1];
return minute;
}
}
return null;
}
/**
* 將標準時間格式HH:mm:ss化爲當前的時間差值
*
* @param str
* @return
*/
public static String StandardFormatStr(String str) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date d = sdf.parse(str);
long timeStamp = d.getTime();
long curTime = System.currentTimeMillis() / (long) 1000;
long time = curTime - timeStamp / 1000;
return time / 60 + "";
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 將一個時間轉換成提示性時間字符串,如剛剛,1秒前
*
* @param timeStamp
* @return
*/
public static String convertTimeToFormat(long timeStamp) {
long curTime = System.currentTimeMillis() / (long) 1000;
long time = curTime - timeStamp;
if (time < 60 && time >= 0) {
return "剛剛";
} else if (time >= 60 && time < 3600) {
return time / 60 + "分鐘前";
} else if (time >= 3600 && time < 3600 * 24) {
return time / 3600 + "小時前";
} else if (time >= 3600 * 24 && time < 3600 * 24 * 30) {
return time / 3600 / 24 + "天前";
} else if (time >= 3600 * 24 * 30 && time < 3600 * 24 * 30 * 12) {
return time / 3600 / 24 / 30 + "個月前";
} else if (time >= 3600 * 24 * 30 * 12) {
return time / 3600 / 24 / 30 / 12 + "年前";
} else {
return "剛剛";
}
}
/**
* 日期變量轉成對應的星期字符串
*
* @param date
* @return
*/
public static final int WEEKDAYS = 7;
//星期字符數組
public static String[] WEEK = {"週日", "週一", "週二", "週三",
"週四", "週五", "週六"};
public static String DateToWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayIndex = calendar.get(Calendar.DAY_OF_WEEK);
if (dayIndex < 1 || dayIndex > WEEKDAYS) {
return null;
}
return WEEK[dayIndex - 1];
}
}