java时间戳获得星期⼏and⽇期获得星期⼏根据⾃⾝实际需要,我有稍作更改。
import java.util.Calendar;
public final class CalendarUtils {
/**
* 将时间戳转换成当天零点的时间戳
*
* @param milliseconds
* @return
*/
private static Calendar zeroFromHour(long milliseconds) {
Calendar calendar = Instance(); // 获得⼀个⽇历
calendar.setTimeInMillis(completMilliseconds(milliseconds));
zeroFromHour(calendar);
return calendar;
}
/**
* 将时,分,秒,以及毫秒值设置为0
*
* @param calendar
*/
private static void zeroFromHour(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
}
/**
* 由于服务器返回的是10位,⼿机端使⽤需要补全3位
*
java时间日期格式转换* @param milliseconds
* @return
*/
private static long completMilliseconds(long milliseconds) {
String milStr = String(milliseconds);
if (milStr.length() == 10) {
milliseconds = milliseconds * 1000;
}
return milliseconds;
}
/**
* 最终调⽤⽅法
* @param timeStamp
* @return
*/
public static String getWhatDay (long timeStamp) {
Calendar cal = FromHour(timeStamp);
String whatDay="";
(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){
whatDay="星期六";
}
(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
whatDay="星期⽇";
}
(Calendar.DAY_OF_WEEK) == Calendar.MONDAY){
whatDay = "星期⼀";
whatDay = "星期⼀";
}
(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY){
whatDay = "星期⼆";
}
(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY){
whatDay = "星期三";
}
(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY){
whatDay = "星期四";
}
(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY){
whatDay = "星期五";
}
return whatDay;
}
}
2、根据⽇期,获得星期⼏
ParseException;
SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/
**
* 注意,传⼊的时间格式必须是2012-12-12这种,中间⽤横杠隔开的,如果⽤/隔开,则需要进⾏调整    */
public class DateTools {
/**
* ⽇期转星期
*
* @param datetime
* @return
*/
public static String dateToWeek(String datetime) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
String[] weekDays = { "星期⽇", "星期⼀", "星期⼆", "星期三", "星期四", "星期五", "星期六" };
Calendar cal = Instance(); // 获得⼀个⽇历
Date datet = null;
try {
datet = f.parse(datetime);
cal.setTime(datet);
} catch (ParseException e) {
e.printStackTrace();
}
int w = (Calendar.DAY_OF_WEEK) - 1; // 指⽰⼀个星期中的某天。
if (w < 0)
w = 0;
return weekDays[w];
}
public static void main(String[] args) {
System.out.println(DateTools.dateToWeek("2017-01-01"));
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。