Java Date from String getDateFromString(String s)

Here you can find the source of getDateFromString(String s)

Description

get Date From String

License

Open Source License

Declaration

public static long getDateFromString(String s) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;

public class Main {
    public static long getDateFromString(String s) {
        int day, month, year;
        int i = s.indexOf('/');
        if (i < 0)
            return 0;
        try {//from   ww w.j a  v a2  s. c o  m
            day = Integer.parseInt(s.substring(0, i));
        } catch (NumberFormatException e) {
            return 0;
        }
        int j = s.indexOf('/', i + 1);
        try {
            month = Integer.parseInt(s.substring(i + 1, j));
        } catch (NumberFormatException e) {
            return 0;
        }
        try {
            year = Integer.parseInt(s.substring(j + 1));
        } catch (NumberFormatException e) {
            return 0;
        }
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, day);
        resetHours(c);
        return c.getTimeInMillis();
    }

    public static void resetHours(Calendar c) {
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
    }
}

Related

  1. convertToDateTimeString(Date d1)
  2. convertToDateWithTimeString(Date date)
  3. getDateFromPattern(String pattern)
  4. getDateFromReccurenceInterval(int reqDOW, int reqDOM, int time)
  5. getDateFromStartTime(String startTime)
  6. getDateFromString(String strDate, String strTime)
  7. getDateFromString(String stringDate)
  8. getDateFromTime(long millisSinceMidnight)