Java String to Date toDate(String dateStr, String pattern)

Here you can find the source of toDate(String dateStr, String pattern)

Description

to Date

License

LGPL

Declaration

public static Date toDate(String dateStr, String pattern) 

Method Source Code

//package com.java2s;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;

public class Main {
    public static java.util.Date toDate(String date, String time, String split) {

        if (date == null || time == null)
            return null;
        String month;//from w  w w . ja v  a 2  s.c  o m
        String day;
        String year;
        String hour;
        String minute;
        String second;

        int dateSlash1 = date.indexOf(split);
        int dateSlash2 = date.lastIndexOf(split);

        if (dateSlash1 <= 0 || dateSlash1 == dateSlash2)
            return null;
        int timeColon1 = time.indexOf(":");
        int timeColon2 = time.lastIndexOf(":");

        if (timeColon1 <= 0)
            return null;
        year = date.substring(0, dateSlash1);
        month = date.substring(dateSlash1 + 1, dateSlash2);
        day = date.substring(dateSlash2 + 1);
        hour = time.substring(0, timeColon1);

        if (timeColon1 == timeColon2) {
            minute = time.substring(timeColon1 + 1);
            second = "0";
        } else {
            minute = time.substring(timeColon1 + 1, timeColon2);
            second = time.substring(timeColon2 + 1);
        }

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

    public static java.util.Date toDate(String monthStr, String dayStr, String yearStr, String hourStr,
            String minuteStr, String secondStr) {

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

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

    public static Date toDate(String dateStr, String pattern) {

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    public static java.util.Date toDate(int month, int day, int year, 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 new java.util.Date(calendar.getTime().getTime());
    }
}

Related

  1. toDate(String date, String format)
  2. toDate(String date, String format)
  3. toDate(String dateStr)
  4. toDate(String dateStr)
  5. toDate(String dateStr, String format)
  6. toDate(String dateString)
  7. toDate(String dateString)
  8. toDate(String dateString)
  9. toDate(String dateString, Locale locale)