Java SQL Time dateTimeFormat(Date date,String pattern)

Here you can find the source of dateTimeFormat(Date date,String pattern)

Description

date Time Format

License

Apache License

Declaration

public static String dateTimeFormat(Date date,String pattern) throws ParseException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

import java.util.Date;

public class Main {
    public static String dateTimeFormat(Date date, String pattern) throws ParseException {
        String s = "";
        try {//w  ww.  java 2 s. c o  m
            SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            s = dformat.format(date);
            s = String.valueOf(StrToTimestamp(s, pattern));
            s = s.substring(0, pattern.length());
        } catch (Exception e) {
        }
        return s;
    }

    public static String dateTimeFormat(Date date) throws ParseException {
        String s = "";
        try {
            SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd");
            s = dformat.format(date);
            s = String.valueOf(StrToTimestamp(s, "yyyy-MM-dd"));
            s = s.substring(0, "yyyy-MM-dd".length());
        } catch (Exception e) {
        }
        return s;
    }

    public static Timestamp StrToTimestamp(String timestampStr, String pattern) throws ParseException {
        java.util.Date date = null;
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            date = format.parse(timestampStr);
        } catch (ParseException ex) {
            throw ex;
        }
        return date == null ? null : new Timestamp(date.getTime());
    }
}

Related

  1. compareTime(String time1, String time2)
  2. createTime(int hour, int minute, int second)
  3. dateTime2str(java.sql.Date date)
  4. DateTimeSpace(String starttime, String endtime)
  5. dateTimeToStr(java.util.Date date)
  6. daytime()
  7. deserializeSqlTime(String text)