Calendar 得到前一天当前时间
@Test
public void test(){
//因为Calendar的构造方法是私有的,所以实例化一个Calendar对象用getInstance方法
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1); //得到前一天
Date date = calendar.getTime();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(df.format(date));
}
这是一个非常好的实例,易于理解,能够帮助我们轻松掌握Calendar的使用方法。可以看点源码,底层使用Map集合实现的
public Map<String, Integer> getDisplayNames(int field, int style, Locale locale) {
if (!checkDisplayNameParams(field, style, ALL_STYLES, LONG, locale,
ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
return null;
}
// ALL_STYLES
if (style == ALL_STYLES) {
Map<String,Integer> shortNames = getDisplayNamesImpl(field, SHORT, locale);
if (field == ERA || field == AM_PM) {
return shortNames;
}
Map<String,Integer> longNames = getDisplayNamesImpl(field, LONG, locale);
if (shortNames == null) {
return longNames;
}
if (longNames != null) {
shortNames.putAll(longNames);
}
return shortNames;
}
// SHORT or LONG
return getDisplayNamesImpl(field, style, locale);
}
private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) {
DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
String[] strings = getFieldStrings(field, style, symbols);
if (strings != null) {
Map<String,Integer> names = new HashMap<String,Integer>();
for (int i = 0; i < strings.length; i++) {
if (strings[i].length() == 0) {
continue;
}
names.put(strings[i], i);
}
return names;
}
return null;
}
Calendar 得到前一天当前时间的更多相关文章
- 如何利用java得到当前的时间和前一天的时间
1.得到当前的时间 Calendar cal = Calendar.getInstance(); String time=formatter.format(cal.getTime()); ...
- Java获取当前日期的前一个月,前一天的时间
Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -); //得到前一天 calendar.add(Cal ...
- Java Calendar.set 方法设置时间的问题
因项目需要,需要遍历一年中的其中几个月,获得每个月的用户数量. 变量有:开始时间--startDate,结束时间--endDate. 逻辑很简单:获取到开始时间的月份和结束时间的月份,然后得到月份差 ...
- Calendar 日历类的时间操作
我们经常会涉及到对时间的处理,例如登陆网站,我们会看到网站首页显示XXX,欢迎您!今天是XXXX年....某些网站会记录下用户登陆的时间,比如银行的一些网站,对于这些经常需要处理的问题,Java中提供 ...
- Java-小技巧-004-jdk时间,jdk8时间,joda,calendar,获取当前时间前一周、前一月、前一年的时间
1.推荐使用java8 localdate等 线程安全 支持较好 地址 2.joda 一.简述 查看SampleDateFormat源码,叙述有: * Date formats are not syn ...
- 使用PHP函数输出前一天的时间和后一天的时间
1.明确date()函数和time()函数的功能,其中time()函数是获取时间戳函数 2.输出前一天的当前时间: echo '一天之前的时间为:'.date('Y-m-d H:i:s',time() ...
- Calendar.getInstance()获取指定时间点(定时)
public class Test1 { public static void main(String[]args){ System.out.println("时间为:\n"+ge ...
- day18 时间:time:,日历:calendar,可以运算的时间:datatime,系统:sys, 操作系统:os,系统路径操作:os.path,跨文件夹移动文件,递归删除的思路,递归遍历打印目标路径中所有的txt文件,项目开发周期
复习 ''' 1.跨文件夹导包 - 不用考虑包的情况下直接导入文件夹(包)下的具体模块 2.__name__: py自执行 '__main__' | py被导入执行 '模块名' 3.包:一系列模块的集 ...
- PHP 打印前一天的时间
时间格式为 2000-02-02 02:02:02 echo date('Y-m-d H:i:s', strtotime( '-1 day', time() ) ):
随机推荐
- java_13.1 javaAPI
1 API概念 API:是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件的以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节.2 String类的概念和不变性 Stri ...
- bootstrap 折叠collapse失效
手动点击折叠,然后调用折叠全部以后,在手动点击折叠项,折叠失效. 方法,折叠项是通过添加或删除".in"来实现,实现如下 $(".collapse.in").c ...
- 安装nginx流程
1.下载nginx压缩包: 下载nginx:http://nginx.org/en/download.html 本教程下载 nginx-1.14.0.zip(http://nginx.org/down ...
- 软件开发中 SQL SERVER 任务的用法
在软件开发中,经常性会用到定时任务.这个时候你可能会想到线程.但是事实中,线程方法比较麻烦.容易出错,资源竞争等问题,设计起来让你很头痛. 现在给大家提供一个新的思路,用SQL SERVER 的任务管 ...
- Oracle的SQL语句中如何处理‘&’符号
‘&’符号在SQL中有特殊含义,所以在SQL中想要写入&,需要特殊处理. 如下SQL语句就不能正确运行: SQL> select 'a&b' from dual; 处理方 ...
- How to install VCM 2 Ford IDS 109 software
How to install Ford IDS 109: 1- Install the ids 86 before changing the date to 1 07 2015 (hold the d ...
- RNA-seq流程需要进化啦!
RNA-seq流程需要进化啦! Posted on 2015年9月25日 Tophat 首次被发表已经是6年前 Cufflinks也是五年前的事情了 Star的比对速度是tophat的50倍,hisa ...
- web前端学习笔记:文本属性
今天的web前端笔记主要讲述文本属性,希望能帮助到正在学习web前端开发的初学者们,废话不多说了,一起来看看文本属性的相关内容吧. 文本属性 文本缩进 将Web页面上的一个段落第一行缩进,这是一种最常 ...
- c#while循环注意continue的地方
在使用while 时发现一个很大的问题,continue最好慎用! private void do() { int i = 0; while (true) { //continue;//绝对的死循环 ...
- git舍弃文件更改
未进行任何提交,即文件更改在工作区 # filename 对应进行操作的文件名 git checkout -- filename 已用git add 命令提交,即文件更改在暂存区 # 舍弃暂存区的修改 ...