Android Date String Parse str2Date(String str)

Here you can find the source of str2Date(String str)

Description

str Date

Declaration

public static java.sql.Date str2Date(String str) 

Method Source Code

//package com.java2s;

import java.util.Calendar;

import java.util.GregorianCalendar;

public class Main {
    public static java.sql.Date str2Date(String str) {
        if (str == null)
            return null;
        str = str.trim();//from  ww w  .j  ava  2  s . c  o m
        if (!str.matches("^\\d\\d\\d\\d-\\d\\d-\\d\\d$")
                && !str.matches("^\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d$"))
            return null;

        String[] ss = str.split("-| |:");
        GregorianCalendar gc = new GregorianCalendar();
        gc.set(Calendar.YEAR, Integer.parseInt(ss[0]));
        gc.set(Calendar.MONTH, Integer.parseInt(ss[1]) - 1);
        gc.set(Calendar.DAY_OF_MONTH, Integer.parseInt(ss[2]));
        if (ss.length == 6) {
            gc.set(Calendar.HOUR_OF_DAY, Integer.parseInt(ss[3]));
            gc.set(Calendar.MINUTE, Integer.parseInt(ss[4]));
            gc.set(Calendar.SECOND, Integer.parseInt(ss[5]));
        }
        return new java.sql.Date(gc.getTimeInMillis());
    }
}

Related

  1. parse(String dateStr)
  2. parse(String input)
  3. parse(String source, String pattern)
  4. parseDate(String value, String[] parsePatterns)
  5. parseJsonDate(String jsonDate)
  6. stringConvertDate(String date)
  7. stringToDate(String date)
  8. toDate(String string)
  9. tokenizeDate(String date)