Java用于取得当前日期相对应的月初,月末,季初,季末,年初,年末时间
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用于取得当前日期相对应的月初,月末,季初,季末,年初,年末时间的更多相关文章
- java时间工具类型,格式化时间,最近7天 月初 月末 季度 月度 时间格式化 等等
package com.tz.util; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util. ...
- java parse 带英文单词的日期字符串 转 date (转化新浪微博api返回的时间)
拂晓风起 专注前端技术cocos2d.js.flash.html5,联系:kenkofox@qq.com.如果读者要找腾讯工作机会,请不吝推荐简历. 博客园 首页 新闻 新随笔 联系 管理 订阅 随笔 ...
- Java,Calendar -- 获取当前日期、当月月初日期、月末日期
public class CalendarTest { public static void main(String[] args) { // 获取当前年份.月份.日期 Calendar cale = ...
- java如何获取当前日期和时间
System.currentTimeMillis() 获取标准时间可以通过System.currentTimeMillis()方法获取,此方法不受时区影响,得到的结果是时间戳格式的.例如: 15431 ...
- java获得系统当前日期
package com.web.test; import java.text.ParseException; import java.text.SimpleDateFormat; import jav ...
- 【Thinking in Java】编写构造器时应注意:尽量避免调用其他非private方法
最近重温了<Thinking in Java>,发现了一个让我为之兴奋的知识漏洞,必须得分享一下. 上一篇的<Java类初始化的过程>的随笔中,那个初始化顺序并不完整.初始化的 ...
- Java 读取Properties文件时应注意的路径问题
1. 使用Class的getResourceAsStream()方法读取Properties文件(资源文件)的路径问题: InputStream in = this.getClass().getRe ...
- Java中获取当前日期
java.util.Date date = new Date();java.Text.SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd ...
- 【java】获取当前日期时间:java.util.Date
public class TestDate { public static void main(String[] args) { System.out.println(new java.util.Da ...
随机推荐
- 【环境】VS2013和MATLAB相互调用混合编程
Visual Studio和MATLAB混合编程,有两种方法: 1 MATLAB调用C程序: 2 VS调用MATLAB(目前见到的都是VS,其他编译器如codeblocks,或不提供这项功能): 前一 ...
- PHP中如何连接数据库基本语句
只是后端修改页面,不需要在前端显示的可以删除原有代码只输入<?php 开始编写语言即可,后面的?>也可以省略 //造一个连接$connect = @mysql_connect(" ...
- 【POJ】【3680】Intervals
网络流/费用流 引用下题解: lyd: 首先把区间端点离散化,设原来的数值i离散化后的标号是c[i].这样离散化之后,整个数轴被分成了一段段小区间. 1.建立S和T,从S到离散化后的第一个点连容量K, ...
- NYOJ-206 矩形的个数 AC 分类: NYOJ 2013-12-29 22:19 265人阅读 评论(0) 收藏
这题目是小学奥数题目,方法可以百度到,但是,有个难点就是,数据类型大小不够,如果是1000x1000的矩阵,那么就会超过int的范围,所以,就引进了long long的数据类型 #include< ...
- WebService流行框架之Axis和CXF
转自:http://www.cnblogs.com/snake-hand/archive/2013/06/09/3129915.html 前言 上节课我们对WebService进行了简单的介绍,对于其 ...
- Using Hooks
The following code examples demonstrate how to perform the following tasks associated with hooks: In ...
- Unity3d之音效播放和调用手机震动
http://blog.csdn.net/sunshine_1984/article/details/12943979 今天研究了下Unity3d音效播放相关内容,整理下实现细节. 1,添加音效文件到 ...
- logback日志配置文件代码示例
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true&q ...
- HDU4945 2048(dp)
先是看错题意..然后知道题意之后写了发dp..无限TLE..实在是不知道怎么优化了,跑了遍数据是对的,就当作理论AC掉好了.. #pragma warning(disable:4996) #inclu ...
- POJ 2028
#include <iostream> #define MAXN 200 using namespace std; int mark[MAXN]; int main() { //freop ...