java calendar获取年_Java Calendar获取年、月、日、时间,设置年、月、日

java calendar获取年_Java Calendar获取年、月、日、时间,设置年、月、日JavaCalendar获取年、月、日、时间Calendarc=Calendar.getInstance(TimeZone.getTimeZone(“GMT+08:00”));//获取东八区时间intyear=c.get(Calendar.YEAR);//获取年intmonth=c.get(Calendar.MONTH)+1;//获取月份,0表示1月份intday=c.get(Calendar.DAY_OF_MONTH);//获取当前

大家好,又见面了,我是你们的朋友全栈君。

Java Calendar获取年、月、日、时间

Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));    //获取东八区时间

int year = c.get(Calendar.YEAR);    //获取年

int month = c.get(Calendar.MONTH) + 1;   //获取月份,0表示1月份

int day = c.get(Calendar.DAY_OF_MONTH);    //获取当前天数

int first = c.getActualMinimum(c.DAY_OF_MONTH);    //获取本月最小天数

int last = c.getActualMaximum(c.DAY_OF_MONTH);    //获取本月最大天数

int time = c.get(Calendar.HOUR_OF_DAY);       //获取当前小时

int min = c.get(Calendar.MINUTE);          //获取当前分钟

int xx = c.get(Calendar.SECOND);          //获取当前秒

SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String curDate = s.format(c.getTime());  //当前日期

System.out.println("当前时间:"+year + "-" + month + "-"+ day + " "+time + ":" + min +":" + xx);

System.out.println("第一天和最后天:" + first +"," + last);

System.out.println("当前日期curDate====:" + curDate);

输出结果:

当前时间:2012-9-25 22:50:54

第一天和最后天:1,30

当前日期curDate:2012-09-25 22:50:54

Calendar的计算

c.add(Calendar.YEAR, 1);

c.add(Calendar.MONTH, 1);

c.add(Calendar.DAY_OF_MONTH, 1);

int year2 = c.get(Calendar.YEAR);

int month2 = c.get(Calendar.MONTH) + 1;

int day2 = c.get(Calendar.DAY_OF_MONTH);

int firstD = c.getActualMinimum(c.DAY_OF_MONTH);

int lastD = c.getActualMaximum(c.DAY_OF_MONTH);

System.out.println("当前时间:"+year2 + "-" + month2 + "-"+ day2 + " "+time + ":" + min +":" + xx);

System.out.println("第一天和最后天:" + firstD +"," + lastD);

输出结果:

当前时间:2013-10-26 23:4:3

第一天和最后天:1,31

获取上个月的年、月、日

Calendar c=Calendar.getInstance();

c.add(Calendar.MONTH, -1);//上个月

SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String year=String.valueOf(c.get(Calendar.YEAR));

String topDay = String.valueOf(c.getActualMinimum(Calendar.DAY_OF_MONTH));

String lastDay = String.valueOf(c.getActualMaximum(Calendar.DAY_OF_MONTH));

//上个月

String lastMonth=String.valueOf(c.get(Calendar.MONTH)+1).length()==2?String.valueOf(c.get(Calendar.MONTH)+1):"0"+String.valueOf(c.get(Calendar.MONTH)+1);

String topDayMonth=year+"-"+lastMonth+"-"+"01" + " 00:00:00";

String lastDayMonth = year+"-"+lastMonth+"-"+lastDay+ " 23:59:59";

System.out.println("###year:" + year);

System.out.println("###last month:" + lastMonth);

System.out.println("###topday:" + topDayMonth);

System.out.println("###lastday:" + lastDayMonth);

输出结果

###year:2013

###last month:04

###topday:2013-04-01 00:00:00

###lastday:2013-04-30 23:59:59

设置年月日

Calendar c = Calendar.getInstance();  // 当前时间的日历对象。
// 设置年,月,日
c.set(2021, 6, 3);
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/144815.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 自编码器原理和实现

    自编码器原理和实现自编码器一、原理:将图像进行压缩,压缩的特征图能够保存原图像的主要特征,即根据特征图能够再次恢复原始图像。二、具体实现方法:自编码器分为两部分:编码和解码。编码可以使用任一卷积网络,可以根据训练数据选择,像MNIST手写数字可以选用简单的神经网络,比如LeNet。解码部分就是反向的神经网络,这样输入和输出图像大小相同,可以直接利用误差平方作为损失函数进行训练。三、实验结果:(1)生成20幅图像:当然这里肯定是要输入20幅原始图像,然后才能查看生成的图像,否则自己设定的隐空间变量生成的图像可能没有

    2022年10月1日
    3
  • ubuntu安装go语言_go语言web服务器

    ubuntu安装go语言_go语言web服务器来源:微信公众号「编程学习基地」文章目录简介下载go安装包环境配置添加环境变量第一个go语言程序go入门学习简介go语言是一种开源的、语法精简的静态编程语言,它的开源社区比较庞大,应用场景非常广范。可以用于系统监控、容器技术(Docker)、大数据、存储技术、分布式系统(HyperledgerFabric)、消息系统(Kafka客户端)、服务器管理、安全工具、Web工具等。这里介绍在Linux上安装并配置go。下载go安装包到GoLang中国,下载Go语言安装包。解压tar-xzv..

    2022年10月12日
    2
  • Idea激活码最新教程2024.3.2.1版本,永久有效激活码,亲测可用,记得收藏

    Idea激活码最新教程2024.3.2.1版本,永久有效激活码,亲测可用,记得收藏Idea 激活码教程永久有效 2024 3 2 1 激活码教程 Windows 版永久激活 持续更新 Idea 激活码 2024 3 2 1 成功激活

    2025年5月31日
    4
  • c语言数组截取,C# 字符串按数组截取方法(C/S)

    c语言数组截取,C# 字符串按数组截取方法(C/S)privatevoidbutton1_Click(objectsender,EventArgse){string[]aa=this.GetYouhouComment(“aaa王ああああああddddd”,4);}///元文字列///桁数///取得する結果privatestring[]GetYouhouComment(stringPValue,intPLenth)…

    2022年6月11日
    64
  • 前缀索引

    前缀索引当索引是很长的字符序列时,这个索引将会很占内存,而且会很慢,这时候就会用到前缀索引了。所谓的前缀索引就是去索引的前面几个字母作为索引,但是要降低索引的重复率,索引我们还必须要判断前缀索引的重复率。先看这样一张表:mysql>select*fromtest;+———-+——-+|name|score|+——–…

    2022年5月24日
    39
  • Object转Map和Map转Object(对象转map,map转对象)

    Object转Map和Map转Object(对象转map,map转对象)第一种方法:fastJosnimportcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;Object转MapFindArchiveDtofindArchiveDto=newFindArchiveDto();findArchiveDto.setContractStatus(“降龙”);…

    2022年4月28日
    437

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号