当前位置: 首页 > 编程日记 > 正文

日期处理工具类 -【二】

1、返回本周的第一天(周日为每周第一天)

 1 /**
 2  * 返回本周的第一天(周日为每周第一天)
 3  * @return
 4  */
 5 public static String getTheFirstDayOfThisWeek(){
 6     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 7     Calendar cal = Calendar.getInstance();
 8     cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
 9     cal.add(Calendar.WEEK_OF_YEAR, 0);
10     String firstDay = format.format(cal.getTime());
11     return firstDay;
12 }

2、返回上周的第一天(周日为每周第一天)

 1 /**
 2  * 返回上周的第一天(周日为每周第一天)
 3  * @return
 4  */
 5 public static String getTheFirstDayOfLastWeek(){
 6     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 7     Calendar cal = Calendar.getInstance();
 8     cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
 9     cal.add(Calendar.WEEK_OF_YEAR, -1);
10     String firstDay = format.format(cal.getTime());
11     return firstDay;
12 }

3、返回上周的最后一天(周六为每周的最后一天)

 1 /**
 2  * 返回上周的最后一天(周六为每周的最后一天)
 3  * @return
 4  */
 5 public static String getTheLastDayOfLastWeek(){
 6     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 7     Calendar cal = Calendar.getInstance();
 8     cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
 9     cal.add(Calendar.WEEK_OF_YEAR, -1);
10     String firstDay = format.format(cal.getTime());
11     return firstDay;
12 }

4、返回本月的第一天

 1 /**
 2  * 返回本月的第一天
 3  * @return
 4  */
 5 public static String getTheFirstDayOfThisMonth(){
 6     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 7     Calendar cal = Calendar.getInstance();
 8     cal.add(Calendar.MONTH, 0);
 9     cal.set(Calendar.DAY_OF_MONTH, 1);
10     String firstDay = format.format(cal.getTime());
11     return firstDay;
12 }

5、返回上个月第一天

 1 /**
 2  * 返回上个月第一天
 3  * @return
 4  */
 5 public static String getTheFirstDayOfLastMonth(){
 6     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
 7     Calendar cal = Calendar.getInstance();//获取当前日期 
 8     cal.add(Calendar.MONTH, -1);
 9     cal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期即为本月第一天 
10     String firstDay = format.format(cal.getTime());
11     return firstDay;
12 }

6、返回上个月最后一天

 1 /**
 2  * 返回上个月最后一天
 3  * @return
 4  */
 5 public static String getTheLastDayOfLastMonth(){
 6     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 7     Calendar cale = Calendar.getInstance();   
 8     cale.set(Calendar.DAY_OF_MONTH, 0);//设置为1号,当前日期既为本月第一天 
 9     String lastDay = format.format(cale.getTime());
10     return lastDay;
11 }

7、返回今年的第一天(即当前年份的1月1日)

 1 /**
 2  * 返回今年的第一天(即当前年份的1月1日)
 3  * @return
 4  */
 5 public static String getTheFirstDayOfThisYear(){
 6     Calendar calendar = Calendar.getInstance();  
 7     calendar.clear();
 8     String year = toString(new Date(), "yyyy");
 9     calendar.set(Calendar.YEAR, Integer.valueOf(year));  
10     Date currYearFirst = calendar.getTime();
11     String firstDay = toString(currYearFirst, "yyyy-MM-dd");
12     return firstDay;
13 }

8、返回昨天的日期

 1 /**
 2  * 返回昨天的日期
 3  * @return
 4  */
 5 public static String getYesterdayDate(){
 6     Calendar cal = Calendar.getInstance();
 7     cal.add(Calendar.DATE, -1);
 8     String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
 9     return yesterday;
10 }

转载于:https://www.cnblogs.com/Huangjw/p/5501817.html

相关文章:

超越对手pdf_如何创建一个超越竞争对手的移动应用

超越对手pdfThe amount of time people spend on their mobile phones has increased over the years, and so has the number of people using mobile devices.多年来,人们在手机上花费的时间增加了,使用移动设备的人数也增加了。 It’s safe to say t…

vue路由对象($route)参数简介

路由对象在使用了 vue-router 的应用中,路由对象会被注入每个组件中,赋值为 this.$route ,并且当路由切换时,路由对象会被更新。 so , 路由对象暴露了以下属性: 1.$route.path 字符串,等于当前路由对象的路…

join......on 后面的and 和where的区别

a.where 是在两个表join完成后,再附上where条件。   b. and 则是在表连接前过滤A表或B表里面哪些记录符合连接条件,同时会兼顾是left join还是right join。即   假如是左连接的话,如果左边表的某条记录不符合连接条件,那么它不…

block的运用

cell的.h文件 typedef void(^ActivityCellBlock)(NSString *str); interface ActivityCell : UITableViewCell property (nonatomic,strong) NSArray *labelAry; property (nonatomic,copy) ActivityCellBlock myBlock; -(void)showCell:(ActivityCellBlock)myBlock; cel…

如何在Ubuntu 20.04上设置Python虚拟环境

I recently got myself a “new” laptop – a Lenovo x270 (yay)! And once again I needed to set up a Python virtual environment. So of course I Googled for a solution, just to find my previously written article on the same topic!我最近给自己买了一台“新”笔记…

getURLParameters - 网址参数

返回包含当前URL参数的对象。 通过适当的正则表达式,使用 String.match() 来获得所有的键值对, Array.reduce() 来映射和组合成一个单一的对象。 将 location.search 作为参数传递给当前 url。 const getURLParameters url >url.match(/([^?&])…

block的使用

#import "ViewController.h" /* 使用Block最大的一个好处就是可以在代码块中随时访问外部变量 比如你在A.class类中的某个方法中声明了一段代码块.你可以在代码块中直接对A.class所拥有的成员变量进行调用,并且,通过一定的条件(__block),还可以随时的修改这…

关于二级菜单的问题

大家在做二级菜单的时候经常会碰到鼠标移出一级菜单,二级菜单瞬间消失,根本不给你机会移到二级菜单上,今天分享下怎样解决这个问题。 ①第一种介绍一种简单粗暴的方法,二级菜单的元素放入一级菜单中。 代码地址:http:h…

python打印换行符_Python换行符以及如何在不使用换行符的情况下进行Python打印

python打印换行符Welcome! The new line character in Python is used to mark the end of a line and the beginning of a new line. Knowing how to use it is essential if you want to print output to the console and work with files.欢迎! Python中的新行字…

tomcat启动后 项目运行缓慢,要几十到几百秒不等 怎么样./startup.sh 运行加快

修改 linux系统中 /usr/local/jdk1.8.0_11/jre/lib/security/java.security 借力 好文章。我们新的Linux系统,部署了多个 Tomca,同时重启后t, 每次都阻塞差不多260秒左右。修改之后总的启动时间下降到6-8秒左右。另外,不确定为什么&#xff0…

转载 C#中使用结构来传递多个参数

C#中当参数超过5个时,建议用结构来传递多个参数。 示例代码如下: 1 public struct MyStruct2 {3 public string str;4 public int number;5 }6 7 class Program8 {9 static void Main(string[] args) 10 { 11 MyStruct myStruct…

xmpp 开源项目选择_如何选择和维护安全的开源项目

xmpp 开源项目选择评估开源项目安全性的一些技巧。 (A few tricks for assessing the security of an open source project.) There is a rather progressive sect of the software development world called the open source community. 在软件开发领域,有一个相当…

【2018-01-22】HTML-表单及表单元素

<body><!--表单--><form action"" method"post"><!--文本类--><input type"text" value"" placeholder"请输入用户名"/><!--文本框--><input type"password" value"…

取消tableView多余的横线

- (void)setExtraCellLineHidden: (UITableView *)tableView{UIView *view [UIView new];view.backgroundColor [UIColor clearColor];[tableView setTableFooterView:view];}

iOS 自定义UITabBar

推荐一篇非常好的集成各种UITabBar的三方库 《点击这里直取demo》 另外一篇根据runtime定制了一款可以出轨的UITarBar 《Runtime实战之定制TabBarItem大小》 点击view的触发机制《iOS事件分发机制&#xff08;一&#xff09; hit-Testing》 摘自&#xff1a; UIView中提供两个…

react入门代码_如何在React中构建温度控制应用程序-包括提示和入门代码

react入门代码我们正在建立的 (What were building) In this beginner React project, were going to learn how to use state hooks, handle events, apply CSS based on state, and more! Check it out: 在这个初学者的React项目中&#xff0c;我们将学习如何使用状态挂钩&am…

决策树(chap3)Machine Learning In Action学习笔记

优点&#xff1a;计算复杂度不高&#xff0c;输出结果易于理解&#xff0c;对中间值的缺失不敏感&#xff0c;可以处理不相关特征数据。缺点&#xff1a;可能会产生过度匹配问题。适用数据类型&#xff1a;数值型&#xff08;必须离散化&#xff09;和标称型。决策树创建分支的…

BigdCIMAL类型数据的使用选择

现在常用的数值类型有Integer , Double , Float , BigDecimal几种 , 常用的当然要数前两种 了 , Integer代表的是整数类型的数据 , double则是代表的是浮点型 , 双精度 ,double的计算精度相对于float来讲要 高 , BigDecimal的计算精度则是最高的 . 可是BigDecimal的一些计算方…

数字字符串转化为时间字符串

(NSString *)dateStringFromNumberTimer:(NSString *)timerStr {//转化为Doubledouble t [timerStr doubleValue];//计算出距离1970的NSDateNSDate *date [NSDate dateWithTimeIntervalSince1970:t];//转化为 时间格式化字符串NSDateFormatter *df [[NSDateFormatter alloc]…

git 覆盖本地修改_Git拉力–如何使用Git覆盖本地更改

git 覆盖本地修改When you learn to code, sooner or later youll also learn about Version Control Systems. And while there are many competing tools in this space, one of them is the de facto standard used by almost everyone in the industry. Its so popular tha…

云计算大会记录

一、要点及主要技术内容记录消费金融刘志军 马上消费大额 分散 小额 短期OpenStack OpenStack是一个由NASA&#xff08;美国国家航空航天局&#xff09;和Rackspace合作研发并发起的&#xff0c;以Apache许可证授权的自由软件和开放源代码项目。 OpenStack是一个开源的云计算管…

获取iOS版本号

(double)getCurrentIOS {return [[[UIDevice currentDevice] systemVersion] doubleValue];}

spring boot 服务 正确关闭方式

引言 Spring Boot&#xff0c;作为Spring框架对“约定优先于配置(Convention Over Configuration)”理念的最佳实践的产物&#xff0c;它能帮助我们很快捷的创建出独立运行、产品级别的基于Spring框架的应用&#xff0c;大部分Spring Boot应用只需要非常少的配置就可以快速运行…

如何在5美元的Raspberry Pi上构建个人开发服务器

In this article, youll learn how to build a personal dev server by installing Git, Node.js, Rust, and Docker on a Raspberry Pi. The cheapest option costs just $5. You can get a starter kit ($25) for free here.在本文中&#xff0c;您将学习如何通过在Raspberry…

Eclipse:xml文件中添加.xsd约束文件

今天在使用dubbo的时候&#xff0c;XML文件一直报错。找不到dubbo的xsd约束文件。 cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element dubbo:reference 解决方法&#xff1a; 找到dubbo的jar包&#xff0c;然后在META-…

029 浏览器不能访问虚拟机的问题解决

1.在搭建分布式时 ssh一直不能进行scp&#xff0c;后来发现一个问题。 windows中的hosts配置了三台虚拟机的映射&#xff0c;但是在虚拟机中的hosts没有配置。 做法是在每台虚拟机上都配置三台虚拟机的映射。 2.端口访问与防火墙 最近帮别人解决问题时才注意的。 以前安装好虚拟…

获取 一个文件 在沙盒Library/Caches/ 目录下的路径

(NSString *)getFullPathWithFile:(NSString *)urlName {//先获取 沙盒中的Library/Caches/路径NSString *docPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];NSString *myCacheDirectory [docPath stringByAppendingPat…

如何有效使用每一点脑力总结_如何更有效地节省脑力和编码

如何有效使用每一点脑力总结如果您知道这些工具的存在&#xff0c;那么您现在可能会使用它们。 (If you knew these tools existed, youd probably be using them by now.) This article isn’t going to tell you about saving your neck with a Roost stand, or your wrists …

C语言程序设计50例(一)(经典收藏)

【程序1】题目&#xff1a;有1、2、3、4个数字&#xff0c;能组成多少个互不相同且无重复数字的三位数&#xff1f;都是多少&#xff1f;1.程序分析&#xff1a;可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去      掉不满足条件的排列。 1 #include…

python多线程执行类中的静态方法

在python 中如果通过多线程的方式执行某个方法很简单&#xff0c;只需要把同步函数的第一个参数为该函数对象即可。但是如果函数对象是某个类的静态方法&#xff0c;这时候如果直接使用类的该函数对象会报错。此时需要构造一个代理的方法来实现。 如&#xff1a;上一个博文中的…