获取当前日期和星期几
工具类:
package web.util;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class TimeUtils {
/**
* 描述 获取当前日期 格式:(yyyy-MM-dd hh:mm:ss)
*
* @param
* @return String
* @author ***
* @date 2018/11/26 10:33
*/
public static String getCurrentFullTime() {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
/**
* 描述 获取当前日期 格式:(yyyy-MM-dd)
*
* @param
* @return String
* @author ***
* @date 2018/11/26 10:33
*/
public static String getCurrentDate() {
LocalDate currentDate = LocalDate.now();
return String.valueOf(currentDate);
}
/**
* 描述 获取今天星期几
*
* @param
* @return String
* @author ***
* @date 2018/11/26 10:53
*/
public static String getDayOfTheWeek() {
String[][] strArray = {{"MONDAY", "一"}, {"TUESDAY", "二"}, {"WEDNESDAY", "三"}, {"THURSDAY", "四"}, {"FRIDAY", "五"}, {"SATURDAY", "六"}, {"SUNDAY", "日"}};
LocalDate currentDate = LocalDate.now();
String k = String.valueOf(currentDate.getDayOfWeek());
//获取行数
for (int i = 0; i < strArray.length; i++) {
if (k.equals(strArray[i][0])) {
k = strArray[i][1];
break;
}
}
return "星期" + k;
}
}
main方法测试:
/**
* 获取当前日期和星期几
* @param args
*/
public static void main(String[] args) {
String currentDate = TimeUtils.getCurrentDate();
String currentFullTime = TimeUtils.getCurrentFullTime();
String dayOfTheWeek = TimeUtils.getDayOfTheWeek();
System.out.println(currentDate);//2019-07-18
System.out.println(currentFullTime);//2019-07-18 15:19:38
System.out.println(dayOfTheWeek);//星期四
}