Java Timestamp Create toTimestamp(String dateTime)

Here you can find the source of toTimestamp(String dateTime)

Description

to Timestamp

License

Open Source License

Declaration

public static Timestamp toTimestamp(String dateTime) 

Method Source Code


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

import java.sql.Timestamp;

public class Main {

    public static Timestamp toTimestamp(String dateTime) {
        java.util.Date newDate = toDate(dateTime);

        if (newDate == null)
            return null;

        return new Timestamp(newDate.getTime());
    }//from   w w w .  j av a  2s.com

    public static Timestamp toTimestamp(String date, String time) {
        java.util.Date newDate = toDate(date, time);

        if (newDate == null)
            return null;

        return new Timestamp(newDate.getTime());
    }

    public static Timestamp toTimestamp(String yearStr, String monthStr, String dayStr, String hourStr,
            String minuteStr, String secondStr) {
        java.util.Date newDate = toDate(yearStr, monthStr, dayStr, hourStr, minuteStr, secondStr);

        if (newDate == null)
            return null;

        return new Timestamp(newDate.getTime());
    }

    public static Timestamp toTimestamp(int year, int month, int day, int hour, int minute, int second) {
        java.util.Date newDate = toDate(year, month, day, hour, minute, second);

        if (newDate == null)
            return null;

        return new Timestamp(newDate.getTime());
    }

    public static java.util.Date toDate(String dateTime) {
        int index = dateTime.indexOf(" ");
        String date = dateTime.substring(0, index);
        String time = dateTime.substring(index + 1);

        return toDate(date, time);
    }

    public static java.util.Date toDate(String date, String time) {
        if (date == null || time == null)
            return null;

        int dateSlash1 = date.indexOf("-");
        int dateSlash2 = date.lastIndexOf("-");

        if (dateSlash1 <= 0 || dateSlash1 == dateSlash2)
            return null;

        int timeColon1 = time.indexOf(":");
        int timeColon2 = time.lastIndexOf(":");

        if (timeColon1 <= 0 || timeColon1 == timeColon2)
            return null;

        String year = date.substring(0, dateSlash1);
        String month = date.substring(dateSlash1 + 1, dateSlash2);
        String day = date.substring(dateSlash2 + 1);

        String hour = time.substring(0, timeColon1);
        String minute = time.substring(timeColon1 + 1, timeColon2);
        String second = time.substring(timeColon2 + 1);
        ;

        return toDate(year, month, day, hour, minute, second);
    }

    public static java.util.Date toDate(String yearStr, String monthStr, String dayStr, String hourStr,
            String minuteStr, String secondStr) {
        int year, month, day, hour, minute, second;

        try {
            year = Integer.parseInt(yearStr);
            month = Integer.parseInt(monthStr);
            day = Integer.parseInt(dayStr);
            hour = Integer.parseInt(hourStr);
            minute = Integer.parseInt(minuteStr);
            second = Integer.parseInt(secondStr);
        } catch (Exception e) {
            return null;
        }
        return toDate(year, month, day, hour, minute, second);
    }

    public static java.util.Date toDate(int year, int month, int day, int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();

        try {
            calendar.set(year, month - 1, day, hour, minute, second);
        } catch (Exception e) {
            return null;
        }
        return calendar.getTime();
    }

    public static java.util.Date toDate(int[] dates) {
        if (dates == null || dates.length < 6)
            return null;

        return toCalendar(dates).getTime();
    }

    public static Calendar toCalendar(String yearStr, String monthStr, String dayStr, String hourStr,
            String minuteStr, String secondStr) {
        int year, month, day, hour, minute, second;

        try {
            year = Integer.parseInt(yearStr);
            month = Integer.parseInt(monthStr);
            day = Integer.parseInt(dayStr);
            hour = Integer.parseInt(hourStr);
            minute = Integer.parseInt(minuteStr);
            second = Integer.parseInt(secondStr);
        } catch (Exception e) {
            return null;
        }

        return toCalendar(year, month, day, hour, minute, second);
    }

    public static Calendar toCalendar(String datetime) {
        int index = datetime.indexOf(" ");
        String date = datetime.substring(0, index);
        String time = datetime.substring(index + 1);

        int dateSlash1 = date.indexOf("-");
        int dateSlash2 = date.lastIndexOf("-");

        if (dateSlash1 <= 0 || dateSlash1 == dateSlash2)
            return null;

        int timeColon1 = time.indexOf(":");
        int timeColon2 = time.lastIndexOf(":");

        if (timeColon1 <= 0 || timeColon1 == timeColon2)
            return null;

        String yearStr = date.substring(0, dateSlash1);
        String monthStr = date.substring(dateSlash1 + 1, dateSlash2);
        String dayStr = date.substring(dateSlash2 + 1);

        String hourStr = time.substring(0, timeColon1);
        String minuteStr = time.substring(timeColon1 + 1, timeColon2);
        String secondStr = time.substring(timeColon2 + 1);
        ;

        int year, month, day, hour, minute, second;

        try {
            year = Integer.parseInt(yearStr);
            month = Integer.parseInt(monthStr);
            day = Integer.parseInt(dayStr);
            hour = Integer.parseInt(hourStr);
            minute = Integer.parseInt(minuteStr);
            second = Integer.parseInt(secondStr);
            Calendar calendar = Calendar.getInstance();

            calendar.set(year, month - 1, day, hour, minute, second);
            return calendar;
        } catch (Exception e) {
            return null;
        }
    }

    public static Calendar toCalendar(int[] dates) {
        if (dates == null || dates.length < 6)
            return null;

        return toCalendar(dates[0], dates[1], dates[2], dates[3], dates[4], dates[5]);
    }

    public static Calendar toCalendar(int year, int month, int day, int hour, int minute, int second) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DATE, day);
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, second);

        return c;
    }
}

Related

  1. toTimestamp(long value)
  2. toTimestamp(Object dateobj)
  3. toTimestamp(Object objInParam)
  4. toTimestamp(String _sDate)
  5. toTimestamp(String dateTime)
  6. toTimestamp(String dt)
  7. toTimestamp(String s)
  8. toTimeStamp(String sDate, String format)
  9. toTimestamp(String str, String format)