java 根據日期取得星期

goldtime發表於2007-09-02

摘自:http://www.blogjava.net/wangxinsh55/archive/2006/12/08/86303.html

方法一、
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]);
}
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/778728/viewspace-967576/,如需轉載,請註明出處,否則將追究法律責任。

相關文章