如何得到当前一周和一个月的第一天? [英] How to get the first day of the current week and month?

查看:103
本文介绍了如何得到当前一周和一个月的第一天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几件大事EX $ P $日期pssed中的毫秒 [1],我想知道哪些事件是本周和本月内,但我不能找出如何获得运行一周的第一天(天/月/年),并将其转换为毫秒,同样对于该月的第一天

  [1]自1970年1月1日00:00:00 GMT
 

解决方案

本周以毫秒为单位:

  //获得今天天气晴朗的时候
日历CAL = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,0); //!明确不会重置一天中的小时!
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);

//获取本周以毫秒为单位开始
cal.set(Calendar.DAY_OF_WEEK,cal.getFirstDayOfWeek());
的System.out.println(从这个星期开始的:+ cal.getTime());
的System.out.println(...毫秒:+ cal.getTimeInMillis());

//开始下一周
cal.add(Calendar.WEEK_OF_YEAR,1);
的System.out.println(开始下周的:+ cal.getTime());
的System.out.println(...毫秒:+ cal.getTimeInMillis());
 

这一个月毫秒:

  //获得今天天气晴朗的时候
日历CAL = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,0); //!明确不会重置一天中的小时!
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);

//获取月份开始
cal.set(Calendar.DAY_OF_MONTH,1);
的System.out.println(开始的一个月的:+ cal.getTime());
的System.out.println(...毫秒:+ cal.getTimeInMillis());

//获取下月开始
cal.add(Calendar.MONTH,1);
的System.out.println(开始下个月的:+ cal.getTime());
的System.out.println(...毫秒:+ cal.getTimeInMillis());
 

I have the date of several events expressed in milliseconds[1], and I want to know which events are inside the current week and the current month, but I can't figure out how to obtain the first day (day/month/year) of the running week and convert it to milliseconds, the same for the first day of the month.

[1]Since January 1, 1970, 00:00:00 GMT

解决方案

This week in milliseconds:

// get today and clear time of day
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);

// get start of this week in milliseconds
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
System.out.println("Start of this week:       " + cal.getTime());
System.out.println("... in milliseconds:      " + cal.getTimeInMillis());

// start of the next week
cal.add(Calendar.WEEK_OF_YEAR, 1);
System.out.println("Start of the next week:   " + cal.getTime());
System.out.println("... in milliseconds:      " + cal.getTimeInMillis());

This month in milliseconds:

// get today and clear time of day
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);

// get start of the month
cal.set(Calendar.DAY_OF_MONTH, 1);
System.out.println("Start of the month:       " + cal.getTime());
System.out.println("... in milliseconds:      " + cal.getTimeInMillis());

// get start of the next month
cal.add(Calendar.MONTH, 1);
System.out.println("Start of the next month:  " + cal.getTime());
System.out.println("... in milliseconds:      " + cal.getTimeInMillis());

这篇关于如何得到当前一周和一个月的第一天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆