java 日期 星期轉(zhuǎn)換 ,獲取
public static List getWeekDayList(String strContainDate) {
List dayList = new ArrayList();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Calendar cpcalendar = (Calendar) calendar.clone();
dayList.add("今天(" + df.format(new Date(cpcalendar.getTimeInMillis()))
+ ")");
cpcalendar.setTime(Date.valueOf(strContainDate));
cpcalendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
dayList.add("星期日(" + df.format(new Date(cpcalendar.getTimeInMillis()))
+ ")");
cpcalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
dayList.add("星期一(" + df.format(new Date(cpcalendar.getTimeInMillis()))
+ ")");
cpcalendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
dayList.add("星期二(" + df.format(new Date(cpcalendar.getTimeInMillis()))
+ ")");
cpcalendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
dayList.add("星期三(" + df.format(new Date(cpcalendar.getTimeInMillis()))
+ ")");
cpcalendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
dayList.add("星期四(" + df.format(new Date(cpcalendar.getTimeInMillis()))
+ ")");
cpcalendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
dayList.add("星期五(" + df.format(new Date(cpcalendar.getTimeInMillis()))
+ ")");
cpcalendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
dayList.add("星期六(" + df.format(new Date(cpcalendar.getTimeInMillis()))
+ ")");
return dayList;
}
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
* @author leo
*
*/
public class DayDate {
/** 星期幾 */
private String day;
/** 日期 */
private String date;
final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
public DayDate() {
super();
DateFormat objDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar objCalendar = Calendar.getInstance();
this.date = objDateFormat
.format(new Date(objCalendar.getTimeInMillis()));
int dayOfWeek = objCalendar.get(Calendar.DAY_OF_WEEK);
this.day = dayNames[dayOfWeek - 1];
}
/**
* 構(gòu)造函數(shù),以字段初始化
*
* @param day
* 設(shè)置星期幾
* @param date
* 設(shè)置日期
*/
public DayDate(String date) {
super();
Calendar objCalendar = Calendar.getInstance();
objCalendar.setTime(Date.valueOf(date));
int dayOfWeek = objCalendar.get(Calendar.DAY_OF_WEEK);
this.day = dayNames[dayOfWeek - 1];
this.date = date;
}
/**
* @return the date
*/
public String getDate() {
return date;
}
/**
* @param date
* the date to set
*/
public void setDate(String date) {
this.date = date;
}
/**
* @return the day
*/
public String getDay() {
return day;
}
/**
* @param day
* the day to set
*/
public void setDay(String day) {
this.day = day;
}
public String toString() {
return day + "(" + date + ")";
}
}
java.util.Date 到 java.sql.Date 的雙向轉(zhuǎn)換
java.sql.Date 是只包含 YYYY-MM-DD 的,而java.util.Date 包含完整的日期和時(shí)間。
// java.sql.Date 轉(zhuǎn) java.util.Date
例如[1]:
java.sql.Date sqlDate = java.sql.Date.valueOf("2010-09-15"); //SQL DATE
long time = sqlDate.getTime(); // getTime() 是各種日期相互轉(zhuǎn)換的橋梁
java.util.Date utilDate = new java.util.Date(time);
//java.util.Date 轉(zhuǎn) java.sql.Date
例如[2]:
java.util.Date utilDate = new java.util.Date(); //獲得系統(tǒng)當(dāng)前時(shí)間
long time = utilDate.getTime(); // getTime() 是各種日期相互轉(zhuǎn)換的橋梁
java.sql.Date sqlDate = new java.sql.Date(time); //SQL DATE
0
0
(請(qǐng)您對(duì)文章做出評(píng)價(jià))