Java判断当前日期是星期几

参考链接地址:http://blog.csdn.net/a9529lty/article/details/3206942


/**

* 判断当前日期是星期几

* @param pTime 修要判断的时间

* @return dayForWeek 判断结果

* @Exception 发生异常

*/

public static int dayForWeek(String pTime) throws Exception {

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

            Calendar c = Calendar.getInstance();

            c.setTime(format.parse(pTime));

            int dayForWeek = 0;

            if (c.get(Calendar.DAY_OF_WEEK) == 1) {

                     dayForWeek = 7;

            } else {

                     dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;

            }

            return dayForWeek;

}


/**

* @param dt

* @return 当前日期是星期几

*/

public static String getWeekOfDate(Date dt) {

             String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

             Calendar cal = Calendar.getInstance();

             cal.setTime(dt);

             int w = cal.get(Calendar.DAY_OF_WEEK) - 1;

             if (w < 0)

                       w = 0;

             return weekDays[w];

}


/**

* 根据日期获得星期

*

* @param date

* @return

*/

public static String getWeekOfDate(String date) {

          if (StringUtils.isEmpty(date)) {

                  return "";

           }

           String[] weekDaysName = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };

           DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

           Date dt1;

           try {

                 dt1 = df.parse(date);

                 Calendar calendar = Calendar.getInstance();

                 calendar.setTime(dt1);

                 int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;

                 return weekDaysName[intWeek];

            } catch (ParseException e) {

                e.printStackTrace();

            }

            return "";

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容