Java Timestamp Create getTimestampInEnglish(java.sql.Timestamp t)

Here you can find the source of getTimestampInEnglish(java.sql.Timestamp t)

Description

get Timestamp In English

License

Apache License

Return

a formatted version of the timestamp 2007-04-19 17:41:02.0 becomes April 19th, 2007 5:41pm

Declaration

private static String getTimestampInEnglish(java.sql.Timestamp t) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*/*w  ww.j a  v  a2s  . c o m*/
 * DateTimeUtils.java
 *
 * Created on March 21, 2008, 9:41 AM
 *
 * Copyright 2011 FooBrew, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * @author  Ryan
     * @return  a formatted version of the timestamp
     *
     *  2007-04-19 17:41:02.0  becomes  April 19th, 2007 5:41pm
     */
    private static String getTimestampInEnglish(java.sql.Timestamp t) throws IllegalArgumentException {

        if (t == null)
            return "";

        String s = t.toString();
        if (s.indexOf(".0") > 0)
            s = s.substring(0, s.indexOf(".0"));

        String[] sr = s.split(" ");

        int month = -1;
        int day = -1;
        int hours = -1;

        String mth = "", dy = "", yr = "", hrs = "", mnts = "", ampm = "";

        try {
            String[] sd = sr[0].split("-");
            String[] st = sr[1].split(":");

            try {
                hours = Integer.parseInt(st[0]);
                month = Integer.parseInt(sd[1]);
                day = Integer.parseInt(sd[2]);
            } catch (Exception e) {
                throw new IllegalArgumentException("Invalid hour");
            }

            if (hours < 0)
                throw new IllegalArgumentException("Invalid timestamp");

            if (hours >= 12) {
                hours = hours - 12;
                ampm = "pm";
            } else {
                ampm = "am";
            }

            if (hours == 0)
                hrs = "12";
            else
                hrs = "" + hours;

            mnts = st[1];

            yr = sd[0];
            mth = getMonth(month);
            dy = formatNumberWithExtension(day);

        } catch (IndexOutOfBoundsException ioobe) {
            throw new IllegalArgumentException("Invalid timestamp format");
        }
        return mth + " " + dy + ", " + yr + " " + hrs + ":" + mnts + ampm;
    }

    /**
     * @author  Ryan
     * @return  the String representation of the month param
     */
    private static String getMonth(int month) throws IllegalArgumentException {
        switch (month) {
        case 1:
            return "January";
        case 2:
            return "February";
        case 3:
            return "March";
        case 4:
            return "April";
        case 5:
            return "May";
        case 6:
            return "June";
        case 7:
            return "July";
        case 8:
            return "August";
        case 9:
            return "September";
        case 10:
            return "October";
        case 11:
            return "November";
        case 12:
            return "December";
        default:
            return "";
        }
    }

    /**
     * 
     * @param num
     * @return
     */
    public static String formatNumberWithExtension(int num) {
        int n = num;
        while (n > 10)
            n = n % 10;

        // (1,21,31) st, (2,22) nd, (3,23) rd, (4:20,24:30) th
        String append = "";
        if (n == 1)
            append = (num == 11 ? "th" : "st");
        else if (n == 2)
            append = (num == 12 ? "th" : "nd");
        else if (n == 3)
            append = (num == 13 ? "th" : "rd");
        else
            append = "th";

        return (num + append);
    }
}

Related

  1. getTimestampFromString(String dateStr)
  2. getTimestampFromString(String dateString, String dateFormat)
  3. getTimestampFromString(String id)
  4. getTimestampFromString(String s)
  5. getTimeStampFromString(String timeStamp)
  6. getTimestampLexical(boolean showSeconds, Locale locale)
  7. getTimestampMinusDays(Integer days)
  8. getTimestampNullSafe(final Date ts)
  9. getTimestampOrNull(Object dbResult)