這篇文章主要介紹了java如何根據日期取得星期的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇java如何根據日期取得星期文章都會有所收獲,下面我們一起來看看吧。
方法一、
public static String getFullDateWeekTime(String sDate){
try{
String formater = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat format = new SimpleDateFormat(formater);
Date date=format.parse(sDate);
format.applyPattern("yyyy-MM-dd E HH:mm:ss");
return format.format(date);
}catch(Exception ex){
System.out.println("TimeUtil getFullDateWeekTime"+ex.getMessage());
return "";
}
}
[@more@]方法二、
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test {
public static void main(String[] args) {
final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
"星期六" };
String s = "2006-01-12 16:30";
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar calendar = Calendar.getInstance();
Date date = new Date();
try {
date = sdfInput.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1;
if(dayOfWeek<0)dayOfWeek=0;
System.out.println(dayNames[dayOfWeek]);
}
}
關于“java如何根據日期取得星期”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“java如何根據日期取得星期”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。