Android date format conversion (date to cycle)

Posted by Lars Berg on Thu, 30 Apr 2020 20:55:42 +0200

Recently, I met a requirement in my own project to convert the standard day yyyy MM DD into a specific cycle, that is, the day of the week. By looking up the data, we found that Android has provided us with a powerful date tool Calendar class, through which we can quickly realize the conversion between dates. Here, I have encapsulated it as a tool class, and provided the conversion of two cycle formats of class Chinese / English:

/**
 * Created by moos on 2018/1/6.
 * func:Tool class of time
 */

public class TimeUtils {

    /**
     * func:Get the day of the week by specific date (Chinese style)
     * @param date Standard date
     * @return  What day is today?
     */
    public static String getChineseWeekDay(String date){
        String weekTime = "week";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {

            c.setTime(format.parse(date));

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        switch (c.get(Calendar.DAY_OF_WEEK)){
            case 1:

                weekTime += "day";
                break;
            case 2:

                weekTime += "One";
                break;
            case 3:

                weekTime += "Two";
                break;
            case 4:

                weekTime += "Three";
                break;
            case 5:

                weekTime += "Four";
                break;
            case 6:

                weekTime += "Five";
                break;
            case 7:

                weekTime += "Six";
                break;

            default:
                throw new IllegalArgumentException("Illegal date format");

        }
        return weekTime;

    }

    /**
     * func:Get the day of the week by date (British)
     * @param date Standard date
     * @return  What day is today?
     */
    public static String getEnglishWeekDay(String date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {

            c.setTime(format.parse(date));

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        switch (c.get(Calendar.DAY_OF_WEEK)){
            case 1:

                return "Sunday";
            case 2:

                return "Monday";
            case 3:

                return "Tuesday";
            case 4:

                return "Wednesday";
            case 5:

                return "Thursday";
            case 6:

                return "Friday";
            case 7:

                return "Saturday";

            default:
                throw new IllegalArgumentException("Illegal date format");

        }

    }
}

There are not many codes, which is very convenient to use. It is worth noting that the parameters can only be date strings in standard format. You can set them according to your own needs. It is not difficult to understand the code part. There is not much explanation here. If you have any questions or suggestions, please leave a message or discuss with us. Thank you

Topics: Android