`

获取日期相关值Calendar

阅读更多
package com.lolaage.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
*
* @author huangye 2011-06-27 计算与日期和时间相关的算法类
*/
public class CalendarUtil {

/* 用于计算日期相关 */
private Calendar c = null;

public CalendarUtil() {
c = Calendar.getInstance();
}

/**
*
* @return 今天是星期几(与数据库中规则一一对应)
*/
public int getWeek() {

/*
* Calendar星期算法是:星期天到星期六 1~7 算法实现思路:
* (向前移动2位刚好和数据库中(周天到周6)0~6与数据库存储的算法(周一到周天1~7中的1~6)对应
* ,Calendar中的0代表周天和数据库中的规则(周天)7对应)
*/
int week = c.get(Calendar.DAY_OF_WEEK) - 1;
/* 数据库中使用的算法是:星期一到星期天是 1~7,所以使用以下算法兼容 */
return week = week < 1 ? 7 : week;
}

/**
* 将Timestamp类型格式化为 yyyy-MM-dd HH:mm
*
* @param date
* @return
*/
public static String getNowTimestamp(Timestamp date) {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return dateformat.format(date);
}
public static String getNowDate(Date date) {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return dateformat.format(date);
}

/**
*
* @return 当前的月份
*/
public int getMonth() {
/* 在Calendar中月份1~12月是从0~11计算的,需要转换 */
return c.get(Calendar.MONTH) + 1;
}
public int getDay(){
return c.get(Calendar.DAY_OF_MONTH);
}
/**
*
* @return 当前的年份
*/
public int getYear() {
return c.get(Calendar.YEAR);
}

/**
* 获取当前时间Timestamp
*/
public  Timestamp getNow() {
return new Timestamp(new Date().getTime());
}

/**
* 获取当前时间 java.sql.Date
*/
public java.sql.Date getNowDate() {
return new java.sql.Date(new Date().getTime());
}

/**
* 获取当前时间精确到秒
*/
public String getNowutilDate() {
SimpleDateFormat dateformat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
return dateformat.format(new Date());
}

/**
* 获取当前的日期yyyy-MM-dd
*
* @return
*/
public static String getNowDateDay() {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
return dateformat.format(new Date());
}
public static String getNowDateDayNoBar() {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
return dateformat.format(new Date());
}
public static String getDateDay(Date date) {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
return dateformat.format(date);
}
public static Date getCurrentDate() {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = dateformat.format(new Date());
Date date = null;
try {
date = dateformat.parse(dateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}

/**
* 获取上月的开始的日期 比如:2011-10-01
*
* @return
*/
public String getAfterDateDayStart() {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM");
Date d = new Date();
d.setMonth(d.getMonth() - 1);
return dateformat.format(d) + "-01";
}

/**
* 获取本月的开始的日期 比如:2011-10-01
*
* @return
*/
public String getNowDateDayStart() {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM");
return dateformat.format(new Date()) + "-01";
}

/**
* 获取当前月的日期年和月 比如:2011-10
*
* @return
*/
public String getNowDateMonth() {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM");
return dateformat.format(new Date());
}

/**
* 获取上个月的日期年和月 比如:yyyy-MM
*
* @return
*/
public String getPreNowDateMonth() {
return this.getYear() + "-" + (this.getMonth() - 1) + "";
}

/**
* 获取上个月的日期年和月 比如:yyyy-MM
*
* @return
*/
public String getPreNowDateMonth2() {
if(this.getMonth()<=10){
return this.getYear() + "-0" + (this.getMonth() - 1) + "";
}else{
return this.getYear() + "-" + (this.getMonth() - 1) + "";
}
}

/**
*
* @return 流水号的日期
*/
public String getBickerCode() {
SimpleDateFormat dateformat = new SimpleDateFormat("yyMMdd");
return dateformat.format(new Date());
}

/**
* 验证该oldDate是否合法 不合法取当前时间
*
* @param oldDate
* @return
* @throws ParseException
*/
@SuppressWarnings("unused")
public Timestamp isNormal(Timestamp oldDate) {
boolean b = false;
int Year = oldDate.getYear();
int Month = oldDate.getMonth() + 1;
int date = oldDate.getDate();

Timestamp now = getNow();

if ((Year >= now.getYear() & Year <= 200) & (Month >= 1 & Month <= 12)
& (date >= 1 & date <= 31)) {

b = true;
} else {
b = false;
}
return b == true ? oldDate : now;
}

public static void main(String[] args) {
CalendarUtil date=new CalendarUtil();

System.out.println(date.getCurrentDate());
}

/**
* 计算传入参数的日期的前i个月份(年份+月份)
*
* @param now
* @return
*/
public static String getMonths(int i) {
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, -i);
Date lastMonthSameDay = cal.getTime();// 上个月
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
String dateStr = format.format(lastMonthSameDay);

return dateStr;
}
/**
* 获取前一天的日期
* @return
*/
public java.sql.Date getAfterNowDate(){
Date date=new Date();
Calendar ca=Calendar.getInstance();
ca.add(ca.DATE, -1);
Date Afterdate=ca.getTime();
return new java.sql.Date(Afterdate.getTime());
}
/**
* @param date
* @return
*/
public static String getDateStr(Date date) {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateformat.format(date);
}

        /**
* 获取上一个月的时间点
* @return
*/
public static long getPreviousMonthTime(){
Calendar ca = Calendar.getInstance();//得到一个Calendar的实例 
ca.setTime(new Date());   //设置时间为当前时间 
ca.add(Calendar.MONTH, -1); //月份减1 
return ca.getTimeInMillis();
}

        /**
* 获取评论的时间格式
* @param commentTime
* @return
*/
public static String getDetailCommentDate(long commentTime){
String dateTime = "";
SimpleDateFormat sf = new SimpleDateFormat("yyyy.MM.dd");
Calendar commentCalendar = Calendar.getInstance();
commentCalendar.setTimeInMillis(commentTime);
Calendar currentCalendar = Calendar.getInstance();
currentCalendar.setTime(new Date());
if(commentCalendar.get(Calendar.YEAR) == currentCalendar.get(Calendar.YEAR)){//评论时间和当前时间是同一年
if((currentCalendar.get(Calendar.MONTH)+1) == (commentCalendar.get(Calendar.MONTH)+1)){//评论时间和当前时间是同月
if(currentCalendar.get(Calendar.DATE) - commentCalendar.get(Calendar.DATE)==2){//前天评论的信息
dateTime = "前天";
}else if(currentCalendar.get(Calendar.DATE) - commentCalendar.get(Calendar.DATE)== 1){//昨天评论的信息
dateTime = "昨天";
}else if(currentCalendar.get(Calendar.DATE) == commentCalendar.get(Calendar.DATE)){//当天评论的信息
dateTime = "今天";
}else{//当月其他日期评论的信息
sf = new SimpleDateFormat("MM.dd");
dateTime = sf.format(commentTime);
}
}else{//是同年但不同月份的评论,时间格式为: 04.01
sf = new SimpleDateFormat("MM.dd");
dateTime = sf.format(commentTime);
}
}else{//往年的评论信息显示年份 如:2012.12.1
dateTime = sf.format(commentTime);
}
return dateTime;
}
}
分享到:
评论

相关推荐

    使用Month Calendar获取日期

    使用Month Calendar获取日期 使用Month Calendar获取日期 使用Month Calendar获取日期

    C#的日历控件使用

    C#中有一个日历控件Calendar,但是现在我需要一个可以下拉的日历控件,并且初始时不显示日历,当我点击下拉按钮时才弹出,并且当选择了日期,日历会自动隐藏且选择的日期值会显示到相应的输入框中。显然Calendar控件...

    Java 实现判断今天是工作日、假日、节假日.zip

    * 获取今天日期值 */ public Date getCurrentDate() { return Calendar.getInstance().getTime(); } 另一个方法是: /** * 判断今天是否节假日 * @param date 今天日期 * @return 返回值 true:今天是工作...

    集合框架练习.doc

    通过使用 Calendar.getInstance() 方法可以获取当前日期,然后将日期设置为目标年份的 2 月 1 日,最后使用 add 方法将日期减去 1 天,获取 2 月的最后一天的日期,最后使用 get 方法获取天数。 3. 创建一个 List ...

    jscalendar:简单的 Javascript 日历

    标尺使用 Javascript 形成... 我避免从本地计算机获取日期。 纯 Javascript 日历,完全独立于第三方库是的,我喜欢重新发明轮子。 当前日期时间取自 div.js-calendar 的 data-currentdatetime 值(最好由服务器生成)。

    Web前端开发技术-综合案例(制作年历).pptx

    获取指定年份1月1日的星期值,获取每个月共有多少天。 循环遍历每个月中的日期。 将日期显示到对应的星期下面。 总结 首先讲解了对象的基本概念,然后讲解了如何自定义对象、如何使用内置对象,对值类型和引用类型...

    java时间处理工具类--DateUtils

    * 指定日历字段的值的滚动方向。true:向上滚动 / false:向下滚动 * @return Date */ public Date roll(int field, boolean up) { cal.setTime(this.fiducialDate); cal.roll(field, up); return cal....

    北大青鸟 Java 教材 第7章描述详细,有示例及图解.

    * get():获取指定的日期和时间信息,如月、日、年、小时、分钟和秒。 四、Collection 接口和实现类 Collection 接口是 Java 集合框架的根接口,提供了基本的集合操作方法。常用的实现类有 ArrayList、LinkedList ...

    java.util.Date与java.sql.Date互转及字符串转换为日期时间格式[文].pdf

    可以使用Calendar类来获取当前时间,然后将其转换为java.sql.Timestamp: Calendar calendar = Calendar.getInstance(); java.sql.Timestamp timestamp = new java.sql.Timestamp(calendar.getTimeInMillis()); 四...

    java常用工具类的使用

    而Date的其他构造方法和普通方法的API都不容易实现国际化,因此目前Date类的大多数方法都被标识为过时,表示更灵活的时间类请参考java.util.Calendar。 Date的输出结果是按照国际通用格式输出的,而中国更习惯于...

    Java实验4 Java 常用API的应用.doc

    Calendar类提供了许多有用的方法来操作日期和时间,例如get()方法可以获取当前日期和时间的信息,add()方法可以将日期和时间加上或减去一定的时间间隔,getTime()方法可以将Calendar对象转换为Date类型对象。...

    temperature-calendar:vue Web应用程序以冷度范围显示带有异常的日历。 在节点中废弃应用程序以从Avamet Meteorlogic平台获取每日记录

    有关每日温度和用于计算异常的每周平均值的数据存储在Elastisearch实例中,如果要包括温度/气象站记录数据,只需按照相同的结构插入具有daily_temperature索引的数据即可。 至少包含以下数据的记录: temperature_...

    joda-time-2.3

    1. 易于使用:Calendar让获取"正常的"的日期变得很困难,使它没办法提供简单的方法,而Joda-Time能够 直接进行访问域并且索引值1就是代表January。 2. 易于扩展:JDK支持多日历系统是通过Calendar的子类来实现,这样...

    joda time 工具

    1. 易于使用:Calendar让获取"正常的"的日期变得很困难,使它没办法提供简单的方法,而Joda-Time能够 直接进行访问域并且索引值1就是代表January。 2. 易于扩展:JDK支持多日历系统是通过Calendar的子类来实现,这样...

    joda-time-1.6-src.zip

    1. 易于使用:Calendar让获取"正常的"的日期变得很困难,使它没办法提供简单的方法,而Joda-Time能够 直接进行访问域并且索引值1就是代表January。 2. 易于扩展:JDK支持多日历系统是通过Calendar的子类来实现,这样...

    react-datetime-picker:React 应用程序的日期时间选择器

    React日期时间选择器React 应用程序的日期时间选择器。... 使用onChange道具获取新值。演示可以在sample目录中找到一个最小的演示页面。 也可用!寻找只是一个日期选取器或一个时间选择器? React-DateTime-Picker 可以

    05-calendar

    05第三方API:工作日计划程序正常运行的网站链接: : 显示有关编码的交互式测验。 GitHub存储库链接: : 功能性当用户加载页面时,他们将看到当前日期以及工作日时间段。 当用户单击文本字段时,他们可以在此处键入...

    Oracle9i的init.ora参数中文说明

    说明: 与 NLS_TIME_TZ_FORMAT 相似, 其中的一对值指定 TIMESTAMP 数据类型的默认值, 该类型除存储 YEAR, MONTH 和 DAY 日期值, HOUR, MINUTE 和 SECOND 时间值, 还存储 TIMEZONE_HOUR 和 TIMEZONE_MINUTE。...

    小小的日历程序

    源代码)一个获取日期和时间的小程序 主要是根据系统秒值 也可以根据指定的秒值 换算成目标日期和时间 有借鉴意义

    vue-jlunar-datepicker-一个带有节日和节气术语的中国农历日期选择器组件。-Vue.js开发

    vue-jLunar-datePicker @JinWen Lunar-Date-Picker组件,lightWeight,功能强大,易于使用,带有节日和节假日术语。 在线演示使用de vue-jLunar-datePicker @JinWen Lunar-Date-Picker组件,重量轻,功能强大...测试值

Global site tag (gtag.js) - Google Analytics