Java date processing component joda-time

Disclaimer: This article is xing_star original article, please indicate the source!

This article synchronization from http://javaexception.com/archives/175

Java date processing component joda-time

Usually in the development process, the processing date and time, will basically use SimpleDateFormat to deal with this is also okay, but sometimes wanted to find a strong point of the tools, the date and time on treatment, I expect that, for a given timestamp is a string or time format string can have a strong Api, can easily help me parse out the date, hour, minute, in addition to also know this is the first time stamp corresponds to the number of days of the year , how many days this month, and so on. After some research and found that the third-party Java framework, there is a joda-time, the date and time date processing component resolved to do enhanced function is really powerful, it is recommended for everyone to use.

16 years from now, I use this component in many projects. Next, use a common demand scenario, showing part of joda-time usage.

As shown below, showing the effect I want to achieve the figure of time, 6:47 pm, 9:09 pm, 11:48 am, etc.

The complete code is as follows:

public static String getTimeString2(String time) {
    long timeMills = Long.parseLong(time);
    DateTime dateTime = new DateTime(timeMills);
    int hourOfDay = dateTime.getHourOfDay();
    int hourOfDayOf12 = hourOfDay;
    int minute = dateTime.getMinuteOfHour();
    String extra = "";
    if (hourOfDay == 0) {
        extra = "凌晨";
        hourOfDayOf12 = 12;
    } else if (hourOfDay > 0 && hourOfDay < 12) {
        extra = "上午";
    } else if (hourOfDay >=12 && hourOfDay < 13) {
        extra = "中午";
    } else if (hourOfDay > 13 && hourOfDay <= 18) {
        extra = "下午";
        hourOfDayOf12 = hourOfDay - 12;
    } else if (hourOfDay > 18 && hourOfDay <= 23) {
        extra = "晚上";
        hourOfDayOf12 = hourOfDay - 12;
    }
    String prefixMinute = "";
    if (minute < 10) {
        prefixMinute = "0";
    }
    return extra + hourOfDayOf12 + ":" + prefixMinute + minute;
}

joda-time, the common Api is DateTime, in general, we can pass the time stamp type long DateTime constructor, after which various values ​​can be acquired by the date and time on the target DateTiem

dateTime.getHourOfDay (); // day points
dateTime.getMinuteOfHour (); // within that hour minutes
dateTime.getYear (); // what year
dateTime.getMonthOfYear (); // month
dateTime.getDayOfMonth (); // number of the month is
dateTime.getDayOfYear (); // The first few days of the year
DateTime.Now () getDayOfYear ();. // get the day is the day of the year

Then look at another picture effect, this image shows the effect I expect is to show a specific date, also yesterday, and if it is, then the day, morning noon afternoon of the same day show the specific time.

 

 

The complete code is as follows:

public static String getTimeString(String time) {
    long timeMills = Long.parseLong(time);
    DateTime dateTime = new DateTime(timeMills);
    int year = dateTime.getYear();
    int month = dateTime.getMonthOfYear();
    int dayOfMonth = dateTime.getDayOfMonth();
    int dayOfYear = dateTime.getDayOfYear();
    int nowDayOfYear = DateTime.now().getDayOfYear();
    if (dayOfYear <= nowDayOfYear) {
        if (dayOfYear == nowDayOfYear) {
            return getTimeString2(time);
        } else if (nowDayOfYear - dayOfYear == 1) {
            return "昨天";
        } else {
            return year + "/" + month + "/" + dayOfMonth;
        }
    }
    return year + "/" + month + "/" + dayOfMonth;
}

About to determine whether there is today's judgment is

dateTime.getDayOfYear()
IF (dateTime.getDayOfYear () == DateTime.Now () getDayOfYear ().); // represents it is today

For joda-time, there are many other Api no introduction, this would like to study a time when there is a demand scenario, joda-time for time string parsing is also very strong. This research could it go.

 

Github project address

https://github.com/JodaOrg/joda-time

Guess you like

Origin www.cnblogs.com/xing-star/p/11248979.html