Java SQL Time From getDateTimeString(String format)

Here you can find the source of getDateTimeString(String format)

Description

get Date Time String

License

Open Source License

Declaration

public static String getDateTimeString(String format) 

Method Source Code


//package com.java2s;
import java.util.Calendar;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

public class Main {

    public static String getDateTimeString(String format) {
        return toDateTimeString(new java.util.Date(), format);
    }/*from www  .  jav  a2  s .  co  m*/

    public static String getDateTimeString() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(nowDate());
    }

    public static String toDateTimeString(java.util.Date date) {
        if (date == null)
            return "";

        String dateString = toDateString(date);
        String timeString = toTimeString(date);

        if (dateString == null || timeString == null)
            return "";

        return dateString + " " + timeString;
    }

    public static String toDateTimeString(Calendar calendar) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }

    public static String toDateTimeString(Calendar calendar, String format) {
        return toDateTimeString(calendar.getTime(), format);
    }

    public static String toDateTimeString(long datetime) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(datetime));
    }

    public static String toDateTimeString(long datetime, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new java.util.Date(datetime));
    }

    public static String toDateTimeString(java.util.Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static String toDateTimeString(Timestamp time) {
        long datetime = time.getTime();
        return toDateTimeString(datetime);
    }

    public static String toDateTimeString(java.sql.Date date) {
        long datetime = date.getTime();
        return toDateTimeString(datetime);
    }

    public static java.util.Date nowDate() {
        return new java.util.Date();
    }

    public static String toDateString(java.util.Date date, String spe) {
        if (date == null)
            return "";

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int year = calendar.get(Calendar.YEAR);

        String monthStr = "" + month;
        String dayStr = "" + day;
        String yearStr = "" + year;

        if (month < 10)
            monthStr = "0" + month;

        if (day < 10)
            dayStr = "0" + day;

        return yearStr + spe + monthStr + spe + dayStr;
    }

    public static String toDateString(java.util.Date date) {
        return toDateString(date, "-");
    }

    public static String toDateString(Calendar calendar) {
        return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    }

    public static String toTimeString(java.util.Date date) {
        if (date == null)
            return "";

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        return toTimeString(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE),
                calendar.get(Calendar.SECOND));
    }

    public static String toTimeString(int hour, int minute, int second) {
        String hourStr = "" + hour;
        String minuteStr = "" + minute;
        String secondStr = "" + second;

        if (hour < 10)
            hourStr = "0" + hour;

        if (minute < 10)
            minuteStr = "0" + minute;

        if (second < 10)
            secondStr = "0" + second;

        return hourStr + ":" + minuteStr + ":" + secondStr;
    }
}

Related

  1. getDateTimeBySecond(long timeMillis)
  2. getDateTimeFormat()
  3. getDateTimeRtnTime(Date date, String time)
  4. getDateTimeStr(Date date)
  5. getDateTimeString(java.sql.Date dd)
  6. getDateTimeStringFromCalendar(Calendar calendar)
  7. getDateTimeTypeString(Connection conn)
  8. getDateTimeTypeString(Connection conn)
  9. toDateTime(final java.util.Date d)