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 ...
随机推荐
- web开发--文档下载
GOOGLE在线文档下载地址分享(GOOGLE的文档地址暂不能用了,会放在其它位置..) GOOGLE的在线文档功能好象挂掉了...等找个其它存放的位置把这些文档再上传上去... 存在GOOGLE里面 ...
- 机器学习&&数据挖掘之一:决策树基础认识
决策树入门篇 前言:分类是数据挖掘中的主要分析手段,其任务就是对数据集进行学习并构造一个拥有预测功能的分类模型,用于预测未知样本的类标号,把类标号未知的样本按照某一规则映射到预先给定的类标号中. 分类 ...
- Effective Java总结
规则1. 用静态工厂方法代替构造器 例子: public class Example { } public class StaticFactory { //valueOf/Of/getInstance ...
- 堆(heap)和栈(stack)的区别
转: 一.预备知识―程序的内存分配 一个由c/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)― 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中 ...
- vsm shadowmap format
遇到个奇怪的问题. 在做vsm ,shadowmap format RGBA8 结果正常 RGBA16F 场景不形成阴影的地方变纯黑,因为sm里面这些地方变纯黑(感觉这个好修一些) RGBA32F 阴 ...
- php正则过滤html标签、空格、换行符的代码,提取图片
$descclear = str_replace("r","",$descclear);//过滤换行 $descclear = str_replace(&quo ...
- Somebody That I Used to Know
一.查看歌词 http://baike.baidu.com/view/7925491.htm 二.考量歌词 1.我告诉自己你就是我的挚爱 但你的陪伴却让我倍感孤单 但那就是爱 让我刻骨铭心难以忘怀 既 ...
- 1051: [HAOI2006]受欢迎的牛
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2276 Solved: 1190[Submit][Sta ...
- 输入格式--InputFormat和InputSplit
1)InputFormat的类图: InputFormat 直接子类有三个:DBInputFormat.DelegatingInputFormat和FileInputFormat,分别表示输入文件的来 ...
- iOS项目的完整重命名方法图文教程
原文链接:http://www.cocoachina.com/ios/20150104/10824.html iOS项目的完整重命名方法图文教程 前言:在iOS开发中,有时候想改一下项目的名字,都会遇 ...