Java Timestamp Field getDateTimeString(java.sql.Timestamp ts)

Here you can find the source of getDateTimeString(java.sql.Timestamp ts)

Description

get Date Time String

License

Open Source License

Declaration

public static String getDateTimeString(java.sql.Timestamp ts) 

Method Source Code

//package com.java2s;

public class Main {
    public static String getDateTimeString(java.sql.Timestamp ts) {
        if (ts == null) {
            return "";
        }//from   ww  w.ja  v a 2 s . c  o m
        return Timestamp2DDMMYYYY(ts) + " " + Timestamp2HHMMSS(ts, 1);
    }

    public static String Timestamp2DDMMYYYY(java.sql.Timestamp ts) {
        if (ts == null) {
            return "";
        } else {
            java.util.Calendar calendar = java.util.Calendar.getInstance();
            calendar.setTime(new java.util.Date(ts.getTime()));

            String strTemp = Integer.toString(calendar.get(calendar.DAY_OF_MONTH));
            if (calendar.get(calendar.DAY_OF_MONTH) < 10) {
                strTemp = "0" + strTemp;
            }
            if (calendar.get(calendar.MONTH) + 1 < 10) {
                return strTemp + "/0" + (calendar.get(calendar.MONTH) + 1) + "/" + calendar.get(calendar.YEAR);
            } else {
                return strTemp + "/" + (calendar.get(calendar.MONTH) + 1) + "/" + calendar.get(calendar.YEAR);
            }
        }
    }

    public static String Timestamp2HHMMSS(java.sql.Timestamp ts, int iStyle) {
        if (ts == null) {
            return "";
        }
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        calendar.setTime(new java.util.Date(ts.getTime()));

        String strTemp;
        if (iStyle == 0) {
            strTemp = Integer.toString(calendar.get(calendar.HOUR_OF_DAY));
        } else {
            strTemp = Integer.toString(calendar.get(calendar.HOUR_OF_DAY));
        }

        if (strTemp.length() < 2) {
            strTemp = "0" + strTemp;
        }
        if (calendar.get(calendar.MINUTE) < 10) {
            strTemp += ":0" + calendar.get(calendar.MINUTE);
        } else {
            strTemp += ":" + calendar.get(calendar.MINUTE);
        }
        if (calendar.get(calendar.SECOND) < 10) {
            strTemp += ":0" + calendar.get(calendar.SECOND);
        } else {
            strTemp += ":" + calendar.get(calendar.SECOND);
        }

        if (iStyle != 0) {
            if (calendar.get(calendar.AM_PM) == calendar.AM) {
                strTemp += " AM";
            } else {
                strTemp += " PM";
            }
        }
        return strTemp;
    }
}

Related

  1. getDateTime(Timestamp timestamp)
  2. getDateTimeFromSqlTimestamp(Timestamp timestamp)
  3. getDateTimeFromTimeStamp(Timestamp timestamp)
  4. getDateTimestamp(ResultSet rs, String columnLabel)
  5. getDateTimeStr(Timestamp timestamp)
  6. getDayBorder(Timestamp dateTime, Timestamp timeSlot, boolean end)
  7. getDayEnd(java.sql.Timestamp stamp, int daysLater)
  8. getDayOfMonthBy(Timestamp ts)
  9. getDayOfTime(Timestamp timestamp)