试图获取本周星期一的日期

Trying to get date of Monday in current week(试图获取本周星期一的日期)
本文介绍了试图获取本周星期一的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我已经实现了一种方法来获取本周星期一的日期,并且我已将星期一指定为一周的第一天.

I have implemented a method to get the date of the Monday in the current week, and I have assigned Monday to be the first day of the week.

但是,无论我做什么,它都会返回 Sun Mar 24 15:03:07 GMT 2013.我看不出这里有什么问题.有人能帮忙吗?

But, no matter what I do, it returns Sun Mar 24 15:03:07 GMT 2013. I can't see what the issue is here. Anybody able to help?

public static Date getFirstDayOfWeekDate()
{
    Calendar cal = Calendar.getInstance();
    cal.setFirstDayOfWeek(2);
    cal.set(Calendar.DAY_OF_WEEK,
    cal.getActualMinimum(Calendar.DAY_OF_WEEK));
    Date firstDayOfTheWeek = cal.getTime();
    return firstDayOfTheWeek;
}

推荐答案

试试这个:

public static Date getFirstDayOfWeekDate() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_WEEK,
            cal.getActualMinimum(Calendar.DAY_OF_WEEK));
    Date now = new Date();
    cal.setTime(now);
    int week = cal.get(Calendar.DAY_OF_WEEK);
    return new Date(now.getTime() - 24 * 60 * 60 * 1000 * (week - 1));
}

这篇关于试图获取本周星期一的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to implement RecyclerView with section header depending on category?(如何根据类别实现带有节标题的 RecyclerView?)
How to generate JNI header file in Eclipse(如何在 Eclipse 中生成 JNI 头文件)
Setting a custom HTTP header dynamically with Spring-WS client(使用 Spring-WS 客户端动态设置自定义 HTTP 标头)
Could you technically call the string[] anything in the main method?(从技术上讲,您可以在 main 方法中调用 string[] 吗?)
What is the proper way of setting headers in a URLConnection?(在 URLConnection 中设置标头的正确方法是什么?)
How to overwrite http-header quot;Hostquot; in a HttpURLConnection?(如何覆盖 http-header “主机在 HttpURLConnection 中?)