Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    /***********************************************************************************************
     * ...
     * ********************************************************************************************/

    public static String dateToFormattedString(Date date) {

        String[] suffixes =
                //  0     1     2     3     4     5     6     7     8     9
                { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th",
                        //10   11    12    13    14    15    16    17    18    19
                        "th", "th", "th", "th", "th", "th", "th", "th", "th", "th",
                        //20   21    22    23    24    25    26    27    28    29
                        "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th",
                        //30   31
                        "th", "st" };

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String dayStr = day + suffixes[day];

        SimpleDateFormat simpleDateFormatDay = new SimpleDateFormat("EEEE");
        String formattedDateDay = simpleDateFormatDay.format(date);

        SimpleDateFormat simpleDateFormatRest = new SimpleDateFormat("LLLL");// yyyy");
        String formattedDateRest = simpleDateFormatRest.format(date);

        return formattedDateDay + ", " + dayStr + " " + formattedDateRest;
    }
}