时间工具(jodaTime)
来源:互联网 发布:啥叫大数据 编辑:程序博客网 时间:2024/06/11 03:22
package com.tynet.common.util;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.List;import java.util.Locale;import org.apache.commons.lang3.time.DateUtils;import org.joda.time.DateTime;import org.joda.time.Days;import org.joda.time.Hours;import org.joda.time.Minutes;import org.joda.time.Months;import org.joda.time.Seconds;import org.joda.time.Weeks;import org.joda.time.Years;import org.joda.time.format.DateTimeFormat;/** * 时间工具(jodaTime) * */public class DateUtil extends DateUtils { /** * 转时间类型 */ public static Date toDate(int year, int month, int day) { return new DateTime(year, month, day, 0, 0).toDate(); } /** * 转时间类型 */ public static Date toDate(int year, int month, int day, int hour, int minute) { return new DateTime(year, month, day, hour, minute).toDate(); } /** * 转时间类型 */ public static Date toDate(int year, int month, int day, int hour, int minute, int second) { return new DateTime(year, month, day, hour, minute, second).toDate(); } /** * 转时间类型 * * @param format * 时间格式 * @return */ public static Date toDate(String format, String timeStr) { return DateTimeFormat.forPattern(format).parseDateTime(timeStr).toDate(); } /** * 获取日期 yyyy-MM-dd * * @return */ public static String getDate() { return new DateTime().toString("yyyy-MM-dd"); } /** * 获取时间 HH:mm:ss * * @return */ public static String getTime() { return new DateTime().toString("HH:mm:ss"); } /** * 获取日期和时间 yyyy-MM-dd HH:mm:ss * * @return */ public static String getDateTime() { return getDateTime("yyyy-MM-dd HH:mm:ss"); } /** * 格式化返回当前时间 * * @param format * @return */ public static String getDateTime(String format) { return formatDateTime(new Date(), format); } /** * 返回指定时间的指定格式化字符串 * * @param date * @param format * @return */ public static String formatDateTime(Date date, String format) { return new DateTime(date.getTime()).toString(format); } /** * 获取指定时间是星期几 周一至周日:1至7 * * @param date * @return */ public static int getWeekNo(Date date) { return new DateTime(date.getTime()).getDayOfWeek(); } public static String dateFormate(Date date, String formate) { Locale.setDefault(Locale.CHINA); if (date != null) { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(formate); return sdf.format(date); } else { return null; } } /** * 获取某个月的所有天 * * @param date * @return */ public static List<Date> getDateListByMonth(Date date) { List<Date> list = new ArrayList<Date>(); Calendar c = Calendar.getInstance(); c.setTime(date); int totalDays = c.getActualMaximum(Calendar.DAY_OF_MONTH); for (int i = 1; i <= totalDays; i++) { c.set(Calendar.DAY_OF_MONTH, i); Date d = c.getTime(); list.add(d); } return list; } /** * 在当前时间上加上指定的天数 * * @param date * @param plus * @return */ public static Date getAddDay(Date date, int plus) { return new DateTime(date.getTime()).plusDays(plus).toDate(); } /** * 在当前时间上加上指定的月数 * * @param date * @param plus * @return */ public static Date getAddMonth(Date date, int plus) { return new DateTime(date.getTime()).plusMonths(plus).toDate(); } /** * 在当前时间上加上指定的分钟数 * * @param date * @param plus * @return */ public static Date getAddMinutes(Date date, int plus) { return new DateTime(date.getTime()).plusMinutes(plus).toDate(); } /** * 在当前时间上加上指定的小时数 * * @param date * @param plus * @return */ public static Date getAddHours(Date date, int plus) { return new DateTime(date.getTime()).plusHours(plus).toDate(); } /** * 在当前时间上加上指定的周数 * * @param date * @param plus * @return */ public static Date getAddWeeks(Date date, int plus) { return new DateTime(date.getTime()).plusWeeks(plus).toDate(); } /** * 在当前时间上加上指定的年数 * * @param date * @param plus * @return */ public static Date getAddYears(Date date, int plus) { return new DateTime(date.getTime()).plusYears(plus).toDate(); } /*** * 日期 转指定格式 时间戳 * * @param date * @param dateFormat * @return */ public static long getUnixDate(Date date, String dateFormat) { SimpleDateFormat formatter = new SimpleDateFormat(dateFormat); long ctime = 0; try { ctime = formatter.parse(formatter.format(date)).getTime(); } catch (ParseException e) { e.printStackTrace(); } return ctime; } /** * 判断指定日期是否在当前日期之前 * * @param dateFormat yyyy-mm-dd * @return */ public static boolean judgeDateBefore(String dateFormat) { DateTime d1 = new DateTime(dateFormat); return d1.isBeforeNow(); } /** * 判断指定日期是否在当前日期之后 * * @param dateFormat yyyy-mm-dd * @return */ public static boolean judgeDateAfter(String dateFormat) { DateTime d1 = new DateTime(dateFormat); return d1.isAfterNow(); } /** * 返回两个日期的时间差, * 返回的时间差格式可以是: Calendar.YEAR, Calendar.MONTH, Calendar.DATE, Calendar.HOUR, Calendar.MINUTE, Calendar.SECOND * 为空时,返回week的差 * @param earlyDate * @param lateDate * @param returnTimeFormat * @return time */ public static int getBetweenTime(Date earlyDate, Date lateDate, int returnTimeFormat) { DateTime earlyDateTime = new DateTime(earlyDate); DateTime lateDateTime = new DateTime(lateDate); if (Calendar.YEAR == returnTimeFormat) { return Years.yearsBetween(earlyDateTime, lateDateTime).getYears(); } else if (Calendar.MONTH == returnTimeFormat) { return Months.monthsBetween(earlyDateTime, lateDateTime).getMonths(); } else if (Calendar.DATE == returnTimeFormat) { return Days.daysBetween(earlyDateTime, lateDateTime).getDays(); } else if (Calendar.HOUR == returnTimeFormat) { return Hours.hoursBetween(earlyDateTime, lateDateTime).getHours(); } else if (Calendar.MINUTE == returnTimeFormat) { return Minutes.minutesBetween(earlyDateTime, lateDateTime).getMinutes(); } else if (Calendar.SECOND == returnTimeFormat) { return Seconds.secondsBetween(earlyDateTime, lateDateTime).getSeconds(); } else { return Weeks.weeksBetween(earlyDateTime, lateDateTime).getWeeks(); } } public static void main(String[] args) { DateTime d1 = new DateTime("2016-11-30"); // 和系统时间比 System.out.println(d1.isAfterNow()); System.out.println(d1.isBeforeNow()); System.out.println(d1.isEqualNow()); }}
0 0
- 时间工具(jodaTime)
- JodaTime时间处理工具使用详解
- jodatime jar比较时间
- jodatime时间格式化
- Jodatime
- JodaTime开源时间/日期库
- JodaTime API
- JAVA使用jodatime进行date类型与string类型的时间相互转换
- JodaTime 基础入门
- jodaTime学习用例
- DateTime JodaTime 快捷方便
- j-jodatime.html
- jodaTime一点用法记录
- 浅谈jodaTime 的使用
- 时间工具
- 时间工具
- 时间工具
- jodaTime 优秀的日期框架
- Tmux会话管理(自动补全)
- 分享FlappyBird制作详解【价值139元】
- linux fopen打开大于2G文件出错的解决方法 (踩了一个大坑)
- 关于在activity中使用popuwindow关闭后引起崩溃的解决方法。
- sort
- 时间工具(jodaTime)
- cocos2d-x 3.0 跨平台游戏开发(官方售价550元)
- sum
- 知道csdn 的存在,开通博客,记录自己学习python经历的小发现及历程
- Redirecting an arbitrary Console's Input/Output
- 遗传算法及其java实现
- 2014兄弟连cocos2d-x3.0视频教程
- 字符串翻转
- EmojiFilter 【检测是否有emoji字符】