package com.zrar.date;

import java.util.Calendar;

/**

*

* 描述:此类用于取得当前日期相对应的月初,月末,季初,季末,年初,年末,返回值均为String字符串

* 1、得到当前日期 today()

* 2、得到当前月份月初 thisMonth()

* 3、得到当前月份月底 thisMonthEnd()

* 4、得到当前季度季初 thisSeason()

* 5、得到当前季度季末 thisSeasonEnd()

* 6、得到当前年份年初 thisYear()

* 7、得到当前年份年底 thisYearEnd()

* 8、判断输入年份是否为闰年 leapYear

*

* 注意事项: 日期格式为:xxxx-yy-zz (eg: 2007-12-05)

*

* 实例:

*

* @author pure

*/

public class DateThis {

private int x; // 日期属性:年

private int y; // 日期属性:月

private int z; // 日期属性:日

private Calendar localTime; // 当前日期

public DateThis() {

localTime = Calendar.getInstance();

}

/**

* 功能:得到当前日期 格式为:xxxx-yy-zz (eg: 2007-12-05)

* @return String

* @author pure

*/

public String today() {

String strY = null;

String strZ = null;

x = localTime.get(Calendar.YEAR);

y = localTime.get(Calendar.MONTH) + 1;

z = localTime.get(Calendar.DATE);

strY = y >= 10 ? String.valueOf(y) : ("0" + y);

strZ = z >= 10 ? String.valueOf(z) : ("0" + z);

return x + "-" + strY + "-" + strZ;

}

/**

* 功能:得到当前月份月初 格式为:xxxx-yy-zz (eg: 2007-12-01)

* @return String

* @author pure

*/

public String thisMonth() {

String strY = null;

x = localTime.get(Calendar.YEAR);

y = localTime.get(Calendar.MONTH) + 1;

strY = y >= 10 ? String.valueOf(y) : ("0" + y);

return x + "-" + strY + "-01";

}

/**

* 功能:得到当前月份月底 格式为:xxxx-yy-zz (eg: 2007-12-31)

* @return String

* @author pure

*/

public String thisMonthEnd() {

String strY = null;

String strZ = null;

boolean leap = false;

x = localTime.get(Calendar.YEAR);

y = localTime.get(Calendar.MONTH) + 1;

if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {

strZ = "31";

}

if (y == 4 || y == 6 || y == 9 || y == 11) {

strZ = "30";

}

if (y == 2) {

leap = leapYear(x);

if (leap) {

strZ = "29";

}

else {

strZ = "28";

}

}

strY = y >= 10 ? String.valueOf(y) : ("0" + y);

return x + "-" + strY + "-" + strZ;

}

/**

* 功能:得到当前季度季初 格式为:xxxx-yy-zz (eg: 2007-10-01)

* @return String

* @author pure

*/

public String thisSeason() {

String dateString = "";

x = localTime.get(Calendar.YEAR);

y = localTime.get(Calendar.MONTH) + 1;

if (y >= 1 && y <= 3) {

dateString = x + "-" + "01" + "-" + "01";

}

if (y >= 4 && y <= 6) {

dateString = x + "-" + "04" + "-" + "01";

}

if (y >= 7 && y <= 9) {

dateString = x + "-" + "07" + "-" + "01";

}

if (y >= 10 && y <= 12) {

dateString = x + "-" + "10" + "-" + "01";

}

return dateString;

}

/**

* 功能:得到当前季度季末 格式为:xxxx-yy-zz (eg: 2007-12-31)

* @return String

* @author pure

*/

public String thisSeasonEnd() {

String dateString = "";

x = localTime.get(Calendar.YEAR);

y = localTime.get(Calendar.MONTH) + 1;

if (y >= 1 && y <= 3) {

dateString = x + "-" + "03" + "-" + "31";

}

if (y >= 4 && y <= 6) {

dateString = x + "-" + "06" + "-" + "30";

}

if (y >= 7 && y <= 9) {

dateString = x + "-" + "09" + "-" + "30";

}

if (y >= 10 && y <= 12) {

dateString = x + "-" + "12" + "-" + "31";

}

return dateString;

}

/**

* 功能:得到当前年份年初 格式为:xxxx-yy-zz (eg: 2007-01-01)

* @return String

* @author pure

*/

public String thisYear() {

x = localTime.get(Calendar.YEAR);

return x + "-01" + "-01";

}

/**

* 功能:得到当前年份年底 格式为:xxxx-yy-zz (eg: 2007-12-31)

* @return String

* @author pure

*/

public String thisYearEnd() {

x = localTime.get(Calendar.YEAR);

return x + "-12" + "-31";

}

/**

* 功能:判断输入年份是否为闰年

*

* @param year

* @return 是:true 否:false

* @author pure

*/

public boolean leapYear(int year) {

boolean leap;

if (year % 4 == 0) {

if (year % 100 == 0) {

if (year % 400 == 0) leap = true;

else leap = false;

}

else leap = true;

}

else leap = false;

return leap;

}

}

java获取年初年末_Java用于取得当前日期相对应的月初,月末,季初,季末,年初,年末时间...相关推荐

  1. java获取月末日期_Java用于取得当前日期相对应的月初,月末,季初,季末,年初,年末时间详解...

    package com.zrar.date; import java.util.Calendar; /** * * 描述:此类用于取得当前日期相对应的月初,月末,季初,季末,年初,年末,返回值均为St ...

  2. MySQL获取季初日期_用于取得当前日期相对应的月初,月末,季初,季末,年初,年末时间...

    package com.zrar.date; import java.util.Calendar; /** * * 描述:此类用于取得当前日期相对应的月初,月末,季初,季末,年初,年末,返回值均为St ...

  3. Java用于取得当前日期相对应的月初,月末,季初,季末,年初,年末时间

    package com.zrar.date;2 import java.util.Calendar;3 /**4 * 5 * 描述:此类用于取得当前日期相对应的月初,月末,季初,季末,年初,年末,返回 ...

  4. 取得当前日期相对应的月初,月末,季初,季末,年初,年末

    转自:http://www.atimin.com/read.php/205.htm Atimin's Blog! package vivi.test; import java.util.Calenda ...

  5. java获取系统日期_java怎么获取当前日期

    java获取当前日期的方法:直接实例化位于Java包java.util的Date类即可,如[Date date = new Date();]. System.currentTimeMillis() 获 ...

  6. java 获取区间随机数_Java获取随机数的3种方法

    主要介绍了Java获取随机数的3种方法,主要利用random()函数来实现 方法1 (数据类型)(最小值+Math.random()*(最大值-最小值+1))例: (int)(1+Math.rando ...

  7. java获取页面标签_java获取网页源代码后,提取标签内容……

    java获取网页源代码后,提取标签内容-- 关注:245  答案:2  mip版 解决时间 2021-02-01 09:11 提问者咏bù琂败 2021-01-31 13:49 import java ...

  8. java 获取视频信息_Java获取视频参数信息

    Java获取视频参数信息 资料收集 经过搜索常用的处理视频的方式有两种:xuggler 和 ffmpeg 因ffmpeg 需要使用JNI调用,这里采用xuggler方式 使用xuggler获取视频宽. ...

  9. java获取每月最后一天_java获取每月的最后一天实现方法

    实例如下: public static void main(String[] args) throws ParseException { // 获取当月的天数(需完善) SimpleDateForma ...

最新文章

  1. GitHub开源:一键生成前后端代码神器
  2. 9月推荐 | 精选机器学习文章Top10
  3. ITJ上的一篇论文(E-WsFrame)
  4. leetcode11盛最多水的容器
  5. 中文站最好的WordPress主题推荐
  6. jq取第一个子元素为select_Java修行第036天---MySQL中的子查询,分页语句,三大范式...
  7. 安装依赖以及页面解析
  8. 线程同步(windows平台):信号量
  9. mysql服务性能优化—my.cnf配置说明详解
  10. Linux自学之旅-基础命令(Ext4文件系统)
  11. input框中的文字加下划线
  12. 计算机管理找不到防火墙,win10系统防火墙服务找不到的具体方案
  13. 【设计灵感】产品设计中的灵感来源于日常生活
  14. 为什么要使用 spring?
  15. arcgis 栅格计算器(Spatial Analyst/Raster Calculator)
  16. 基于Python的多功能IP搜索平台设计与实现
  17. PowerDesigner12版本过期解决办法
  18. zemax---中英文名词对照表(持续更新中)
  19. 学习 Linux 内核书籍推荐
  20. 农村承包土地调查数据库规范(试行)

热门文章

  1. JAVA写HTTP代理服务器(三)-https明文捕获
  2. 解决ViewPager缓存导致不能实时刷新数据
  3. 为什么平衡对游戏数值策划如此重要
  4. ECharts开源图表使用方法简单介绍
  5. 下拉列表框Spinner
  6. linux vsftp的配置
  7. IMAP 称作交互邮件访问协议
  8. KafkaManager中Group下不显示对应Topic的解决方案
  9. 在用户控件中弹出消息框的方法
  10. servlet中response中文乱码