Calendar date = Calendar.getInstance();
int nowMonth = date.get(Calendar.MONTH)+1; //月份
int nowYear = date.get(Calendar.YEAR); //年份
大家都知道,Calendar的月份是从0 开始计算的,因此取当前月份nowMonth 需要在后面+1,才可以得到8
但是碰上SimpleDateFormat格式化就不一样了。。这里千万不能弄month+1, 否则输出就不是200908了。
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
String billTime = simpleDateFormat.format(date.getTime());
System.out.println(billTime);
主要还是因为SimpleDateFormat只接受Date()参数,在传递的时候我们用了date.getTime().一旦哪天支持了Calendar的参数,不知道是否需要我们继续手动+1 呢?
分享到:
相关推荐
本源码资源提供了Java中的日期时间处理类相关内容,包括日期、时间和时区的操作方法和示例。它涵盖了Java编程中常用的日期时间处理需求以及如何使用日期时间类来解决这些问题。 适用人群 本源码资源适用于具备一定...
NULL 博文链接:https://qinchaoyong.iteye.com/blog/543109
关于Date、SimpleDateFormat、Calendar 、arraycopy 和 StringBuilder题目小结 对于题目的多种分析与解法
第五章.常用类.java.API概述——Date类,Calendar类,SimpleDateFormat类,Big
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); date.setTime(time); System.out.println(sdf.format(date)); 发现时间于想要的时间不符,请运行Time.reg文件
JavaScript实现的java.text.SimpleDateFormat。希望多多交流。
有关SimpleDateFormat的常用方法说明
日期操作。。。基础的SimpleDateFormat格式化日期!!操作!》初级学习代码
主要介绍了Java时区处理之Date,Calendar,TimeZone,SimpleDateFormat的区别于用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
由浅入深解析 SimpleDateFormat 由浅入深解析 SimpleDateFormat
java 使用SimpleDateFormat类获取系统的当前时间 java 使用SimpleDateFormat类获取系统的当前时间
SimpleDateFormat使用详解。非常实用!!!!
目录SimpleDateFormat诡异bug复现SimpleDateFormat诡异bug字符串日期转Date日期(parse)Date日期转String类型(format)SimpleDateFormat出现bug的原因如何解决SimpleDateFormat多线程安全问题局部变量使用...
高并发之-SimpleDateFormat类的线程安全问题和解决方案.docx
深入理解Java:SimpleDateFormat安全的时间格式化
1.创建SimpleDateFormat对象,确定日期被格式化的格式 2.使用循环,在循环中调用Thread的sleep方法,让线程休眠1s后打印当前时间的字符串
java中时间类Date和Calendar的使用 java中时间类Date和Calendar的使用 ... import java.text.DateFormat; import java.text.ParseException;... import java.text.SimpleDateFormat; 解压密码 www.jiangyea.com
NULL 博文链接:https://chaoyi.iteye.com/blog/2082317
java Date以及相关的Calendar和SimpleDateFormat的使用
NULL 博文链接:https://flynndang.iteye.com/blog/711878