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. java获取系统日期_java怎么获取当前日期

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

  5. java 获取周日期_java 获得本周一到周五的日期

    展开全部 import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public cla ...

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

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

  7. java获取指定日期的所在周的第一天(周一)

    java获取指定日期的所在周的第一天(周一) private static void getWeekByDate(Date time) {SimpleDateFormat sdf = new Simp ...

  8. JAVA获取指定日期

    JAVA获取指定日期 获取前/后(月.天.小时)时间 获取上月: 得到一个月最后一天日期(31/30/29/28) 获取本日日期 获取本周一日期 获取本周日日期 获取本月日期 java项目中经常需要用 ...

  9. Java获取某个日期前后几天日期

    spark,hadoop交流群,QQ群号:521066396,欢迎加入共同学习,一起进步~ 以下是Java获取某个日期前后几天日期的代码: public static void main(String ...

最新文章

  1. vue/require-v-for-key]Elements in iteration expect to have ‘v-bind:key‘ directives
  2. GitHub上拥有4万+star的大佬大厂求职经验分享
  3. 最后一块石头的重量II
  4. C#学习之用迭代器实现枚举器
  5. 虚拟机中访问连接在物理机上的摄像机(使用桥接)
  6. mapinfo图层导入奥维_(通信技能分享)怎样把谷歌地球上画的路线图导入到测试软件中!...
  7. c++导出标准win32格式的dll
  8. LGWR和DBWn的触发条件
  9. 有哪些指标可以描述两个图(graph)的相似度?
  10. php判断值和类型,php如何判断某变量的类型?
  11. css定位中position:absolute与float的区别
  12. python发布代码图片_gitpython模块与代码发布项目流程图
  13. RS485最大通讯距离和RS485接口定义
  14. 风尚云网学习-h5的input:type属性的image属性
  15. 体验美容科技产品:让人不断保养和自我厌恶 | 行业
  16. 心路历程 部分感想
  17. [计算机视觉] (三)相机的针孔成像模型
  18. qpython安装requests库_qpython3安装库的三种方法
  19. 【二维前缀和】小白月赛-秘法地震
  20. 关于树莓派4B的屏幕输入信号源由HDMI变为AV2的处理方法

热门文章

  1. outlook自定义邮件提示声音以及设置接收邮件的间隔时间
  2. Linux+Apache+Postgresql+PHP安装Drupal7.15
  3. 博客文章的置顶功能『博客帮助』
  4. 虚拟机共享文件夹引发的一点思考
  5. 打造DMPO通道完成sdwan优化
  6. CS231n 学习笔记(2)——神经网络 part2 :Softmax classifier
  7. 图解Redis之数据结构篇——压缩列表
  8. 《剑指offer》第十三题(机器人的运动范围)
  9. node Error: Most middleware (like session) is no longer bundled with Express and must be installed
  10. office 2013 安装问题