Explain the common time operation of JAVA
- 2020-05-19 04:58:19
- OfStack
There is often a need to deal with the time in the project, the following is a number of commonly used operations, convenient to use again later and do relevant review.
1. Convert string to date
/**
* String to date
* @param dateStr The date to be converted
* @param dateFormat The date format yyyy-MM-dd/yyyy-MM-dd HH:mm:ss
*/
public static Date toDate(String dateStr, SimpleDateFormat dateFormat) throws ParseException{
Date date = null;
try {
date = dateFormat.parse(dateStr);
} catch (ParseException e) {
logger.debug("Fail to convert String to Date, {}", dateStr);
}
return date;
}
2. Time stamp to date
/**
* Time stamp to date
* @param date
* @return
*/
public static String dateToTime(long time, SimpleDateFormat dateFormat) throws ParseException{
String data = null;
try {
dateFormat.format(new Date(time*1000));
} catch (Exception e) {
logger.debug("Fail to convert long to Date, {}", time);
}
return data;
}
3. Date is formatted as a string
/**
* The date is formatted as a string
* @param date
* @param dateFormat
* @return
* @throws ParseException
*/
public static String toString(Date date, SimpleDateFormat dateFormat) throws ParseException{
return dateFormat.format(date);
}
4. Get the date before or after the specified date, 10 mins is 00:00:00
/**
* Gets the date before or after the specified date
* @param date
* @param num Positive is after, negative is before
* @return yyyy-MM-dd 00:00:00
*/
public static Date getSpecificDate(Date date, int num){
Calendar todayCal = Calendar.getInstance();
todayCal.setTime(date);
Calendar c = Calendar.getInstance();
c.set(todayCal.get(Calendar.YEAR), todayCal.get(Calendar.MONTH), todayCal.get(Calendar.DAY_OF_MONTH) + num, 0, 0, 0);
return c.getTime();
}
Gets the date before or after the specified date, with the current minute and second
/**
* Gets the date before or after the specified date
* @param date
* @param num Positive is before, negative is after
* @return yyyy-MM-dd + The current minute and second
*/
public static Date getSpecificDateAndHhMmSs(Date date,int num){
Calendar c = Calendar.getInstance();
c.setTime(date);
int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day - num);
return c.getTime();
}
6. Converts the time string of type time into hours and minutes
/**
* will time A time string of type Converted to When,
* HH-mm-ss -->> HH-mm
* @param time
* @return
*/
public static String timeToHHMM(String time){
return time.substring(0, time.length() - 3);
}
Get the hours and minutes of a certain date
/**
* Gets the time and minutes of a certain date
* @param date
* @return HH-mm
*/
public static String getHM(Date date){
Calendar ca = Calendar.getInstance();
ca.setTime(date);
Integer hour = ca.get(Calendar.HOUR_OF_DAY);// hours
Integer minute = ca.get(Calendar.MINUTE);// points
String rs_hour = hour.toString();
String rs_minute = minute.toString();
if (rs_hour.length() == 1){
rs_hour = "0" + hour;
}
if(rs_minute.length() == 1){
rs_minute = "0" + minute;
}
return rs_hour + ":" + rs_minute;
}
8. Time string of type time -- > > The number of seconds that start at zero
/**
* time A time string of type -->> The number of seconds that start at zero
* @param time HH-mm / HH-mm-ss
* @return
*/
public static Integer timeToSeconds(String time){
String[] timeSplit = null;
int hours = 0,minutes = 0,seconds = 0;
try {
timeSplit = time.split(":");
if (timeSplit.length == 2) {
hours = Integer.valueOf(timeSplit[0])*60*60;
minutes = Integer.valueOf(timeSplit[1])*60;
}else if(timeSplit.length == 3){
hours = Integer.valueOf(timeSplit[0])*60*60;
minutes = Integer.valueOf(timeSplit[1])*60;
seconds = Integer.valueOf(timeSplit[2]);
}else{
logger.debug("Fail to convert the time, {}", time);
}
} catch (Exception e) {
logger.debug("Fail to convert the time, {}", time);
throw e;
}
return hours + minutes + seconds;
}
9. Rotation time in seconds starting at zero -- > > HH-mm-ss
/**
* The number of revolutions in seconds starting at zero -->> HH-mm-ss
* @param durationSeconds
* @return
*/
public static String getDuration(int durationSeconds){
int hours = durationSeconds /(60*60);
int leftSeconds = durationSeconds % (60*60);
int minutes = leftSeconds / 60;
int seconds = leftSeconds % 60;
StringBuffer sBuffer = new StringBuffer();
sBuffer.append(addZeroPrefix(hours));
sBuffer.append(":");
sBuffer.append(addZeroPrefix(minutes));
sBuffer.append(":");
sBuffer.append(addZeroPrefix(seconds));
return sBuffer.toString();
}
public static String addZeroPrefix(int number){
if(number < 10)
return "0"+number;
else
return ""+number;
}
10. Compare the number of seconds between two dates
/**
* Compare the number of seconds between two dates
* @param startDate
* @param endDate
* @return
*/
public static int getTimeSeconds(Date startDate,Date endDate) {
long a = endDate.getTime();
long b = startDate.getTime();
return (int)((a - b) / 1000);
}
11. Determine whether there is an intersection between two time periods
/**
* Time stamp to date
* @param date
* @return
*/
public static String dateToTime(long time, SimpleDateFormat dateFormat) throws ParseException{
String data = null;
try {
dateFormat.format(new Date(time*1000));
} catch (Exception e) {
logger.debug("Fail to convert long to Date, {}", time);
}
return data;
}
0
12. Get the day of the week (1-7 represents week 1 to Sunday, respectively)
/**
* Time stamp to date
* @param date
* @return
*/
public static String dateToTime(long time, SimpleDateFormat dateFormat) throws ParseException{
String data = null;
try {
dateFormat.format(new Date(time*1000));
} catch (Exception e) {
logger.debug("Fail to convert long to Date, {}", time);
}
return data;
}
1