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)<br>
* @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)<br>
* @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)<br>
* @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)<br>
* @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)<br>
* @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)<br>
* @return String
* @author pure
*/
public String thisYear() {
x = localTime.get(Calendar.YEAR);
return x + "-01" + "-01";
}
/**
* 功能:得到当前年份年底 格式为:xxxx-yy-zz (eg: 2007-12-31)<br>
* @return String
* @author pure
*/
public String thisYearEnd() {
x = localTime.get(Calendar.YEAR);
return x + "-12" + "-31";
}
/**
* 功能:判断输入年份是否为闰年<br>
*
* @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用于取得当前日期相对应的月初,月末,季初,季末,年初,年末时间的更多相关文章

  1. java时间工具类型,格式化时间,最近7天 月初 月末 季度 月度 时间格式化 等等

    package com.tz.util; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util. ...

  2. java parse 带英文单词的日期字符串 转 date (转化新浪微博api返回的时间)

    拂晓风起 专注前端技术cocos2d.js.flash.html5,联系:kenkofox@qq.com.如果读者要找腾讯工作机会,请不吝推荐简历. 博客园 首页 新闻 新随笔 联系 管理 订阅 随笔 ...

  3. Java,Calendar -- 获取当前日期、当月月初日期、月末日期

    public class CalendarTest { public static void main(String[] args) { // 获取当前年份.月份.日期 Calendar cale = ...

  4. java如何获取当前日期和时间

    System.currentTimeMillis() 获取标准时间可以通过System.currentTimeMillis()方法获取,此方法不受时区影响,得到的结果是时间戳格式的.例如: 15431 ...

  5. java获得系统当前日期

    package com.web.test; import java.text.ParseException; import java.text.SimpleDateFormat; import jav ...

  6. 【Thinking in Java】编写构造器时应注意:尽量避免调用其他非private方法

    最近重温了<Thinking in Java>,发现了一个让我为之兴奋的知识漏洞,必须得分享一下. 上一篇的<Java类初始化的过程>的随笔中,那个初始化顺序并不完整.初始化的 ...

  7. Java 读取Properties文件时应注意的路径问题

    1. 使用Class的getResourceAsStream()方法读取Properties文件(资源文件)的路径问题:  InputStream in = this.getClass().getRe ...

  8. Java中获取当前日期

    java.util.Date date = new Date();java.Text.SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd ...

  9. 【java】获取当前日期时间:java.util.Date

    public class TestDate { public static void main(String[] args) { System.out.println(new java.util.Da ...

随机推荐

  1. Oracle 相关概念详解

    一.前言 笔者对于Oracle数据库的理解,很长时间停留在“镜花水月”的状态,你说不懂吧,又会用,一较真起来吧,对一些基本概念又说不出一个道道来~如果想要在编码的路上走得更远,这个必定也是绕不过的坎, ...

  2. [转]Similarities between Hibernate and JDBC objects

  3. 心情符号love

    写点什么呢,先谢谢心情吧,算是第一个脚印了,想先把之前的一些笔记和心得迁移进来吧,以后每个月都要充实自己的知识.向大婶们看齐.走你们走过的脚印,看你们前行的身影.沿着你们留下的路,继续为后者拓宽道路. ...

  4. ASP.NET MVC学习之视图篇(1)

    一.前言 不知道还有多少读者从第一篇开始一直学习到如今,笔者也会一直坚持将ASP.NET MVC的学习完美的结束掉,然后开始写如何配合其他框架使用ASP.NET MVC的随笔.当然笔者后面的随笔如果没 ...

  5. silverlight的第一个程序

    摘要:silverlight是微软公司全力打造的一种跨平台.跨浏览器的RIA新技术,silverlight以XAML为界面呈现语言,支持2D矢量图形.动画.数据绑定.控件风格与模板.LINQ.WCF. ...

  6. RVA与Offset的换算函数

    function RVAToFileOffset(FileName:string; RVA: Cardinal): Cardinal; var   MemPE: TFileStream;   PEDo ...

  7. HDU4542 小明系列故事——未知剩余系

    大赞的数论题: 大致思路: 对于TYPE=1的情况,认为 X 中有 X-K个约数,求最小的X,X-K>0 那么化为B+K的约数为B, 我们知道(B+K)的约数<=2*SQRT(B+K);这 ...

  8. Java NIO 与 基于reactor设计模式的事件处理模型

    Java NIO非堵塞应用通常适用用在I/O读写等方面,我们知道,系统运行的性能瓶颈通常在I/O读写,包括对端口和文件的操作上,过去,在打开一个I/O通道后,read()将一直等待在端口一边读取字节内 ...

  9. Javascript引擎单线程机制及setTimeout执行原理说明

    setTimeout用法在实际项目中还是会时常遇到.比如浏览器会聪明的等到一个函数堆栈结束后才改变DOM,如果再这个函数堆栈中把页面背景先从白色设为红色,再设回白色,那么浏览器会认为DOM没有发生任何 ...

  10. 定时器 corn 表达式

    一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素.按顺序依次为1.秒(0~59)2.分钟(0~59)3.小时(0~23)4.天(月)(0~31,但是你需要考虑你月的天数)5.月(0~11 ...