Joda-Time 工具类的使用

1 篇文章 0 订阅

Joda-time 简单出来时间库。
导入maven 工程jar包

<dependency>  
        <groupId>joda-time</groupId>  
        <artifactId>joda-time</artifactId>  
        <version>2.9.2</version>  
    </dependency>  
//方法一:取系统点间    
DateTime dt1 = new DateTime();    
//方法二:通过java.util.Date对象生成    
DateTime dt2 = new  DateTime(new Date());     
//方法三:指定年月日点分秒生成(参数依次是:年,月,日,时,分,秒,毫秒)    
static DateTime dt3 = new DateTime(2016, 6, 15, 14, 47, 0, 0);  
//方法四:ISO8601形式生成    
DateTime dt4 = new DateTime("2016-06-15");    
DateTime dt5 = new DateTime("2016-06-20T13:14:00");   
//只需要年月日的时候    
LocalDate ld1 = new LocalDate(2016, 6, 15);  
//只需要时分秒毫秒的时候    
LocalTime localTime = new LocalTime(13, 30, 26, 0);// 1:30:26PM    
  
DateTime dt = new DateTime();  
int year = dt.getYear();//年  
int month = dt.getMonthOfYear();//月  
int day = dt.getDayOfMonth(); //天  
int day1 = dt.getDayOfWeek();//天  
int day2 = dt.getDayOfYear();//天  
int week = dt.getDayOfWeek(); //星期     
int hour = dt.getHourOfDay();   //点     
int min = dt.getMinuteOfHour();  //分    
int sec = dt.getSecondOfMinute();  //秒    
int msec = dt.getMillisOfSecond();  //毫秒      

@Override  
public String toString() {  
    System.out.println(year+","+month+","+day+","+day1+","+day2+","+week+","+hour+","+min+","+sec+","+msec);      
    return "";  
}  
public void week(){  
    //星期  
    DateTime dts = new DateTime();   
    switch (dts.getDayOfWeek()) {  
    case DateTimeConstants.SUNDAY:  
        System.out.println("星期日");  
        break;  
          
    case DateTimeConstants.MONDAY:  
        System.out.println("星期一");  
        break;  
          
    case DateTimeConstants.TUESDAY:  
        System.out.println("星期二");  
        break;  
    case DateTimeConstants.WEDNESDAY:  
        System.out.println("星期三");    
        break;  
    case DateTimeConstants.THURSDAY:  
        System.out.println("星期四");  
        break;  
    case DateTimeConstants.FRIDAY:  
        System.out.println("星期五");  
        break;  
    case DateTimeConstants.SATURDAY:  
        System.out.println("星期6");  
    default:  
        break;  
    }  
      
      
}
DateTime dt01 = new DateTime();    
    
//昨天    
DateTime yesterday = dt01.minusDays(1);           
//明天    
DateTime tomorrow = dt01.plusDays(1);         
//1个月前    
DateTime before1month = dt01.minusMonths(1);          
//3个月后    
DateTime after3month = dt01.plusMonths(3);            
//2年前    
DateTime before2year = dt01.minusYears(2);            
//5年后    
DateTime after5year = dt01.plusYears(5);   
  
DateTime d1 = new DateTime("2012-02-01");    
DateTime d2 = new DateTime("2012-05-01");    
    
//和系统时间比    
boolean b1 = d1.isAfterNow();    
boolean b2 = d1.isBeforeNow();    
boolean b3 = d1.isEqualNow();    
    
//和其他日期比    
boolean f1 = d1.isAfter(d2);    
boolean f2 = d1.isBefore(d2);    
boolean f3 = d1.isEqual(d2);    
  
  
DateTime begin = new DateTime("2012-02-01");    
DateTime end = new DateTime("2012-05-01");    
    
//计算区间毫秒数    
Duration d = new Duration(begin, end);    
long time = d.getMillis();    
    
//计算区间天数    
Period p = new Period(begin, end, PeriodType.days());    
int days = p.getDays();    
    
//计算特定日期是否在该区间内    
Interval i = new Interval(begin, end);    
boolean contained = i.contains(new DateTime("2012-03-01"));    
  
//默认设置为日本时间    
  
DateTime dt001 = new DateTime();    
    
//伦敦时间    
DateTime dt002 = new DateTime(DateTimeZone.forID("Europe/London"));    
//月末日期      
DateTime lastday = dt.dayOfMonth().withMaximumValue();    
    
//90天后那周的周一    
DateTime firstday = dt.plusDays(90).dayOfWeek().withMinimumValue();   

}

引用的http://rensanning.iteye.com/blog/1546652

DateUtils 工具类:JodaTime 对时间的加减 处理起来 特别方便 ,快速。自己在工作写了一下公用的方法。

/** 
 * 获取当前系统时间 
 * @return yyyy-MM-dd HH:mm:ss 
 */  
public static String getCurrentTime() {  
    DateTime dt = new DateTime();  
    String time = dt.toString(FORMAT_TIME);  
    return time;  
}  

/** 
 * 获取系统当前时间按照指定格式返回 
 * @param pattern  yyyy/MM/dd hh:mm:a 
 * @return 
 */  
public static String getCurrentTimePattern(String pattern) {  
    DateTime dt = new DateTime();  
    String time = dt.toString(pattern);  
    return time;  
}  

/** 
 * 获取当前日期 
 * @return 
 */  
public static String getCurrentDate() {  
    DateTime dt = new DateTime();  
    String date = dt.toString(FORTER_DATE);  
    return date;  
}  

/** 
 * 获取当前日期按照指定格式 
 * @param pattern 
 * @return 
 */  
public static String getCurrentDatePattern(String pattern) {  
    DateTime dt = new DateTime();  
    String date = dt.toString(pattern);  
    return date;  
}  

/** 
 * 按照时区转换时间 
 * @param date 
 * @param timeZone 时区 
 * @param parrten 
 * @return 
 */  
@Nullable  
public static String format(Date date, TimeZone timeZone, String parrten) {  
    if (date == null) {  
        return null;  
    }  
    SimpleDateFormat sdf = new SimpleDateFormat(parrten);  
    sdf.setTimeZone(timeZone);  
    return sdf.format(date);  
}  

/** 
 * 获取指定时间 
 * @param year 年 
 * @param month 月 
 * @param day 天 
 * @param hour 小时 
 * @param minute 分钟 
 * @param seconds 秒 
 * @return yyyy-MM-dd HH:mm:ss 
 */  
public static String getPointTime(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer seconds) {  
    DateTime dt = new DateTime(year, month, day, hour, minute, seconds);  
    String date = dt.toString(FORMAT_TIME);  
    return date;  
}  

/** 
 * 
 * @param year 年 
 * @param month 月 
 * @param day 天 
 * @param hour 小时 
 * @param minute 分钟 
 * @param seconds 秒 
 * @param parrten 自定义格式 
 * @return parrten 
 */  
public static String getPointTimePattern(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer seconds, String parrten) {  
    DateTime dt = new DateTime(year, month, day, hour, minute, seconds);  
    String date = dt.toString(parrten);  
    return date;  
}  

/** 
 * 获取指定日期 
 * @param year 
 * @param month 
 * @param day 
 * @return 
 */  
public static String getPointDate(Integer year, Integer month, Integer day) {  
    LocalDate dt = new LocalDate(year, month, day);  
    String date = dt.toString(FORTER_DATE);  
    return date;  
}  

/** 
 * 获取指定日期 返回指定格式 
 * @param year 
 * @param month 
 * @param day 
 * @param parrten 
 * @return 
 */  
public static String getPointDatParrten(Integer year, Integer month, Integer day, String parrten) {  
    LocalDate dt = new LocalDate(year, month, day);  
    String date = dt.toString(parrten);  
    return date;  
}  

/** 
 * 获取当前是一周星期几 
 * @return 
 */  
public static String getWeek() {  
    DateTime dts = new DateTime();  
    String week = null;  
    switch (dts.getDayOfWeek()) {  
    case DateTimeConstants.SUNDAY:  
        week = "星期日";  
        break;  

    case DateTimeConstants.MONDAY:  
        week = "星期一";  
        break;  

    case DateTimeConstants.TUESDAY:  
        week = "星期二";  
        break;  
    case DateTimeConstants.WEDNESDAY:  
        week = "星期三";  
        break;  
    case DateTimeConstants.THURSDAY:  
        week = "星期四";  
        break;  
    case DateTimeConstants.FRIDAY:  
        week = "星期五";  
        break;  
    case DateTimeConstants.SATURDAY:  
        week = "星期六";  
    default:  
        break;  
    }  
    return week;  
}  

/** 
 * 获取指定时间是一周的星期几 
 * @param year 
 * @param month 
 * @param day 
 * @return 
 */  
public static String getWeekPoint(Integer year, Integer month, Integer day) {  
    LocalDate dts = new LocalDate(year, month, day);  
    String week = null;  
    switch (dts.getDayOfWeek()) {  
    case DateTimeConstants.SUNDAY:  
        week = "星期日";  
        break;  
    case DateTimeConstants.MONDAY:  
        week = "星期一";  
        break;  
    case DateTimeConstants.TUESDAY:  
        week = "星期二";  
        break;  
    case DateTimeConstants.WEDNESDAY:  
        week = "星期三";  
        break;  
    case DateTimeConstants.THURSDAY:  
        week = "星期四";  
        break;  
    case DateTimeConstants.FRIDAY:  
        week = "星期五";  
        break;  
    case DateTimeConstants.SATURDAY:  
        week = "星期六";  
        break;  

    default:  
        break;  
    }  
    return week;  
}  

/** 
 * 格式化日期 
 * @param date 
 * @return yyyy-MM-dd HH:mm:ss 
 */  
@Nullable  
public static String format(Date date) {  
    if (date == null) {  
        return null;  
    }  
    SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME);  
    return format.format(date);  
}  

/** 
 * 格式化日期字符串 
 * @param date 日期 
 * @param pattern 日期格式 
 * @return 
 */  
@Nullable  
public static String format(Date date, String pattern) {  
    if (date == null) {  
        return null;  
    }  
    SimpleDateFormat format = new SimpleDateFormat(pattern);  
    return format.format(date);  
}  

/** 
 * 解析日期 
 * @param date 日期字符串 
 * @param pattern 日期格式 
 * @return 
 */  
@Nullable  
public static Date parse(String date, String pattern) {  
    if (date == null) {  
        return null;  
    }  
    Date resultDate = null;  
    try {  
        resultDate = new SimpleDateFormat(pattern).parse(date);  
    } catch (ParseException e) {  

    }  
    return resultDate;  
}  

/** 
 * 解析日期yyyy-MM-dd HH:mm:ss 
 * @param date 日期字符串 
 * @return 
 */  
@Nullable  
public static Date parse(String date) {  
    if (date == null) {  
        return null;  
    }  

    try {  
        return new SimpleDateFormat(FORMAT_TIME).parse(date);  
    } catch (ParseException e) {  
        return null;  
    }  
}  

/** 
 * 解析日期 yyyy-MM-dd HH:mm:ss 
 * @param timestamp 
 * @return 
 */  
public static String format(Long timestamp, String pattern) {  
    String dateStr = "";  
    if (null == timestamp || timestamp.longValue() < 0) {  
        return dateStr;  
    }  
    try {  
        Date date = new Date(timestamp);  
        SimpleDateFormat format = new SimpleDateFormat(pattern);  
        dateStr = format.format(date);  
    } catch (Exception e) {  
        // ignore  
    }  

    return dateStr;  
}  

/** 
 * 解析日期 yyyy-MM-dd HH:mm:ss 
 * @param timestamp 
 * @return 
 */  
public static String format(Long timestamp) {  
    String dateStr = "";  
    if (null == timestamp || timestamp.longValue() < 0) {  
        return dateStr;  
    }  
    try {  
        Date date = new Date(timestamp);  
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME);  
        dateStr = format.format(date);  
    } catch (Exception e) {  
        // ignore  
    }  

    return dateStr;  
}  

/** 
 *获取当前时间前几天时间,按指定格式返回 
 * @param days 
 * @return 
 */  
public static String forwardDay(Integer days, String format) {  
    DateTime dt = new DateTime();  
    DateTime y = dt.minusDays(days);  
    return y.toString(format);  
}  

/** 
 *获取当前时间前几天时间 
 * @param days 
 * @return 
 */  
public static Date forwardDay(Integer days) {  
    DateTime dt = new DateTime();  
    DateTime y = dt.minusDays(days);  
    return y.toDate();  
}  

/** 
 * 获取指定时间之后或者之前的某一天00:00:00 默认返回当天 
 * @param days 
 * @return 
 */  
public static Date day00(Integer days, String date, String zimeZone) throws Throwable {  
    DateTime dt;  
    TimeZone timeZone;  
    try {  
        if (StringUtils.isBlank(zimeZone)) {  
            timeZone = TimeZone.getDefault();  
        } else {  
            timeZone = TimeZone.getTimeZone(zimeZone);  
        }  
        if (StringUtils.isBlank(date)) {  
            dt = new DateTime().withZone(DateTimeZone.forTimeZone(timeZone)).toLocalDateTime().toDateTime();  
        } else {  
            dt = new DateTime(date).withZone(DateTimeZone.forTimeZone(timeZone)).toLocalDateTime().toDateTime();  
        }  
    } catch (Exception e) {  
        throw new Throwable(e);  
    }  

    DateTime y = dt.minusDays(days).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0);  
    return y.toDate();  
}  

/** 
 *获取指定时间之后或者之前的某一天23:59:59 默认返回当天 
 * @param days 偏移量 
 * @return 
 */  
public static Date day59(Integer days, String date, String zimeZone) throws Throwable {  
    DateTime dt;  
    TimeZone timeZone;  
    try {  
        if (StringUtils.isBlank(zimeZone)) {  
            timeZone = TimeZone.getDefault();  
        } else {  
            timeZone = TimeZone.getTimeZone(zimeZone);  
        }  
        if (StringUtils.isBlank(date)) {  

            dt = new DateTime().withZone(DateTimeZone.forTimeZone(timeZone)).toLocalDateTime().toDateTime();  
        } else {  
            dt = new DateTime(date).withZone(DateTimeZone.forTimeZone(timeZone)).toLocalDateTime().toDateTime();  
        }  
    } catch (Exception e) {  
        throw new Throwable(e);  
    }  
    DateTime y = dt.minusDays(days).withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59);  
    return y.toDate();  
}  

/** 
 * 计算两个时间相差多少天 
 * @param startDate 
 * @param endDate 
 * @return 
 */  
@Nullable  
public static Integer diffDay(Date startDate, Date endDate) {  
    if (startDate == null || endDate == null) {  
        return null;  
    }  
    DateTime dt1 = new DateTime(startDate);  
    DateTime dt2 = new DateTime(endDate);  
    int day = Days.daysBetween(dt1, dt2).getDays();  
    return Math.abs(day);  
}  

/** 
 * 获取某月之前,之后某一个月最后一天,24:59:59 
 * @return 
 */  
public static Date lastDay(Date date, Integer month) {  
    DateTime dt1;  
    if (month == null) {  
        month = 0;  
    }  
    if (date == null) {  
        dt1 = new DateTime().minusMonths(month);  
    } else {  
        dt1 = new DateTime(date).minusMonths(month);  
    }  
    DateTime lastDay = dt1.dayOfMonth().withMaximumValue().  
            withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59);  
    return lastDay.toDate();  
}  

/** 
 *获取某月月之前,之后某一个月第一天,00:00:00 
 * @return 
 */  
public static Date firstDay(Date date, Integer month) {  
    DateTime dt1;  
    if (month == null) {  
        month = 0;  
    }  
    if (date == null) {  
        dt1 = new DateTime().minusMonths(month);  
    } else {  
        dt1 = new DateTime(date).minusMonths(month);  
    }  
    DateTime lastDay = dt1.dayOfMonth().withMinimumValue().  
            withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0);  
    return lastDay.toDate();  
}  

public static Date addDay(Date date, int offset) {  
    DateTime dt1;  
    if (date == null) {  
        dt1 = new DateTime().plusDays(offset);  
        return dt1.toDate();  
    }  
    dt1 = new DateTime(date).plusDays(offset);  
    return dt1.toDate();  

}  

/** 
 * 传入日期时间与当前系统日期时间的比较, 
 * 若日期相同,则根据时分秒来返回 , 
 * 否则返回具体日期 
 * @return 日期或者 xx小时前||xx分钟前||xx秒前 
 */  
@Nullable  
public static String getNewUpdateDateString(Date now, Date createDate) {  
    if (now == null || createDate == null) {  
        return null;  
    }  
    Long time = (now.getTime() - createDate.getTime());  
    if (time > (24 * 60 * 60 * 1000)) {  
        return time / (24 * 60 * 60 * 1000) + "天前";  
    } else if (time > (60 * 60 * 1000)) {  
        return time / (60 * 60 * 1000) + "小时前";  
    } else if (time > (60 * 1000)) {  
        return time / (60 * 1000) + "分钟前";  
    } else if (time >= 1000) {  
        return time / 1000 + "秒前";  
    }  
    return "刚刚";  
}  
  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Joda-Time 是一个 Java 编程语言的日期和时间处理库,它提供了简化日期和时间操作的功能。使用 Joda-Time,你可以轻松地进行日期和时间的计算、格式化、解析等操作。 下面是 Joda-Time 的一些常见用法: 1. 创建日期对象: ```java DateTime now = new DateTime(); // 创建当前日期和时间对象 DateTime specificDate = new DateTime(2022, 1, 1, 0, 0, 0); // 创建指定日期和时间对象 ``` 2. 获取日期和时间的各个部分: ```java int year = now.getYear(); // 获取年份 int month = now.getMonthOfYear(); // 获取月份 int day = now.getDayOfMonth(); // 获取日期 int hour = now.getHourOfDay(); // 获取小时 int minute = now.getMinuteOfHour(); // 获取分钟 int second = now.getSecondOfMinute(); // 获取秒数 ``` 3. 格式化日期和时间: ```java String formattedDate = now.toString("yyyy-MM-dd"); // 将日期格式化为指定格式的字符串 String formattedTime = now.toString("HH:mm:ss"); // 将时间格式化为指定格式的字符串 String formattedDateTime = now.toString("yyyy-MM-dd HH:mm:ss"); // 将日期和时间格式化为指定格式的字符串 ``` 4. 解析字符串为日期对象: ```java DateTime parsedDate = DateTime.parse("2022-01-01"); // 解析字符串为日期对象 ``` 5. 对日期进行计算和操作: ```java DateTime modifiedDate = now.plusDays(7); // 将日期加上指定天数 DateTime result = now.minusYears(1).plusMonths(3); // 对日期进行复合操作 ``` 以上是 Joda-Time 的一些基本用法,你可以根据自己的需求进一步探索该工具类的其他功能。请注意,Joda-Time 在 Java 8 及更高版本中已经被官方的 java.time 包所取代,因此在使用新的 Java 版本时,你可以直接使用 java.time 包中的类来处理日期和时间。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值