Java 获取当前时间的上个月信息
2017-08-22 18:03
447 查看
获取当前时间上个月的函数getLastYearAndMonth(date)。
/**
* 获取现在时间的上个月信息
* @return “yyyy-MM” (年-月)
* @throws ParseException
*/
public static String getLastYearAndMonth(Date date) throws ParseException{
Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatDate=new SimpleDateFormat(“yyyy-MM”);
String strDate=formatDate.format(date);
Date newDate=formatDate.parse(strDate);
calendar.setTime(newDate);
//取得现在时间
//System.out.println(formatDate.format(curDate));
调用次函数。
public static void main(String[] args) throws ParseException {
Date date=new Date();
//调用获取函数
String strDate=getLastYearAndMonth(date);
System.out.println(strDate);
}
打印结果:
2017-08
/**
* 获取现在时间的上个月信息
* @return “yyyy-MM” (年-月)
* @throws ParseException
*/
public static String getLastYearAndMonth(Date date) throws ParseException{
Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatDate=new SimpleDateFormat(“yyyy-MM”);
String strDate=formatDate.format(date);
Date newDate=formatDate.parse(strDate);
calendar.setTime(newDate);
//取得现在时间
//System.out.println(formatDate.format(curDate));
//取得上一个时间 //calendar.set(Calendar.MONDAY, calendar.get(Calendar.MONDAY) - 1); //取得上一个月 calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)); return formatDate.format(calendar.getTime()); }
调用次函数。
public static void main(String[] args) throws ParseException {
Date date=new Date();
//调用获取函数
String strDate=getLastYearAndMonth(date);
System.out.println(strDate);
}
打印结果:
2017-08
相关文章推荐
- java获取本月开始时间和结束时间、上个月第一天和最后一天的时间以及当前日期往前推一周、一个月
- Java获取时间范围: 当前季度,上个季度,昨天,当前月,上个月
- java获取当前应用的运行信息(内存,线程,运行时间,状态等)
- java获取本月开始时间和结束时间、上个月第一天和最后一天的时间以及当前日期往前推一周、一个月
- Java获取当前时间+中国时区工具类
- 获取系统当前时间和创建TXT文件并写入信息
- java获取当前时间设置时间格式为mysql数据库的datetime类型
- android java获取当前时间的总结
- Java获取当前月的起始和结束时间
- java中获取当前系统时间,日期并格式化输出
- Java获取当前时间,前一天时间
- java使用new Date()和System.currentTimeMillis()获取当前时间戳
- 分享一个获取 当前周的计算方法 时间 Java
- java获取取得Timestamp类型的当前系统时间
- Java基础——获取当前系统时间上一天时间及各月最后一天
- java获取当前系统信息
- JAVA获取当前系统时间的方式
- JAVA中获取当前系统时间
- JAVA获取当前系统时间System.currentTimeMillis()
- Java中Calendar的一些简单操作 及 JS中获取当前时间前一天时间(js中时间Date操作)