Java SQL Date Create convertFormat(String dateData)

Here you can find the source of convertFormat(String dateData)

Description

convert Format

License

Open Source License

Declaration

public static String convertFormat(String dateData) throws Exception 

Method Source Code


//package com.java2s;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {

    public static String convertFormat(String dateData) throws Exception {
        return convertFormat(dateData, "yyyy/MM/dd");
    }/*from  w  w w.  j a  v a2  s.c o  m*/

    public static String convertFormat(String dateData, String format) throws Exception {

        return convertToString(convertToTimestamp(dateData), format);

    }

    public static String convertToString(Timestamp dateData) throws Exception {

        return convertToString(dateData, "yyyy/MM/dd");

    }

    public static String convertToString(Timestamp dateData, String pattern) throws Exception {
        return convertToString(dateData, pattern, java.util.Locale.CHINA);
    }

    public static String convertToString(Timestamp dateData, String pattern, java.util.Locale locale)
            throws Exception {
        try {

            if (dateData == null) {
                return null;
            }

            SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
            //formatter.applyPattern( pattern );

            return formatter.format(dateData);
        } catch (Exception e) {
            throw new Exception("[DateUtil][convertToString]" + e.getMessage(), e);
        }

    }

    public static Timestamp convertToTimestamp(String dateData) throws Exception {

        try {

            if (dateData == null)
                return null;
            if (dateData.trim().equals(""))
                return null;

            int dateObjLength = dateData.length();

            String yearString = "2002";
            String monthString = "01";
            String dayString = "01";

            if (dateObjLength >= 4) {
                yearString = dateData.substring(0, 4);
            }
            if (dateObjLength >= 6) {
                monthString = dateData.substring(4, 6);
            }
            if (dateObjLength >= 8) {
                dayString = dateData.substring(6, 8);
            }

            int year = Integer.parseInt(yearString);
            int month = Integer.parseInt(monthString) - 1;
            int day = Integer.parseInt(dayString);

            Calendar cal = new GregorianCalendar();
            cal.set(year, month, day);
            //cal.getTime();
            return new Timestamp(cal.getTime().getTime());

        } catch (Exception e) {
            throw new Exception("[DateUtil][convertToTimestamp]" + e.getMessage(), e);
        }

    }
}

Related

  1. convertDateFromString(String inputDateString)
  2. convertDateFromUtilToSql(java.util.Date utilDate)
  3. convertDateIfNecessary(Date date)
  4. convertDateToString(java.sql.Date argDate)
  5. convertFormat(java.sql.Date dt, String formatter)
  6. convertiFecha(Date utilFecha)
  7. convertirSQLDate2String(java.sql.Date sqlDate)
  8. convertJavaDate2SqlDate(java.util.Date date)
  9. converToDate(String yyyy, String mm, String dd)