Android Date String Parse dateTimeToDateObj(String date, String time)

Here you can find the source of dateTimeToDateObj(String date, String time)

Description

date Time To Date Obj

Declaration

public static Date dateTimeToDateObj(String date, String time) 

Method Source Code

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

import java.util.Date;
import java.util.StringTokenizer;

public class Main {
    public static Date dateTimeToDateObj(String date, String time) {
        java.util.Calendar cal = java.util.Calendar.getInstance();

        int[] dateTokens;
        dateTokens = tokenizeDate(date);
        cal.set(java.util.Calendar.YEAR, dateTokens[2]);
        cal.set(java.util.Calendar.MONTH, dateTokens[0] - 1);
        cal.set(java.util.Calendar.DATE, dateTokens[1]);

        int[] timeTokens;
        timeTokens = tokenizeTime(time);
        if (timeTokens[0] == 12) {
            timeTokens[0] -= 12;/*from   w w w. j av  a2  s  . co  m*/
        }
        cal.set(java.util.Calendar.HOUR, timeTokens[0]);
        cal.set(java.util.Calendar.MINUTE, timeTokens[1]);
        cal.set(java.util.Calendar.SECOND, 0);
        cal.set(java.util.Calendar.MILLISECOND, 0);
        cal.set(java.util.Calendar.AM_PM, timeTokens[2]);

        return new Date(cal.getTimeInMillis());
    }

    public static int[] tokenizeDate(String date) {
        StringTokenizer st = new StringTokenizer(date, "-/");

        ArrayList<Integer> al = new ArrayList<Integer>();

        while (st.hasMoreTokens()) {
            al.add(Integer.parseInt(st.nextToken()));
        }

        int[] tokenList = new int[3];

        for (int i = 0; i < al.size(); i++) {
            tokenList[i] = al.get(i);
        }

        return tokenList;
    }

    public static int[] tokenizeTime(String time) {
        String newTime = time.replaceAll(":", " ").replaceAll("PM", "1")
                .replaceAll("AM", "0"); // 11:27 PM will become "11 17 1"

        StringTokenizer st = new StringTokenizer(newTime, " ");

        ArrayList<Integer> al = new ArrayList<Integer>();

        while (st.hasMoreTokens()) {
            al.add(Integer.parseInt(st.nextToken()));
        }

        int[] tokenList = new int[3];

        for (int i = 0; i < al.size(); i++) {
            tokenList[i] = al.get(i);
        }

        return tokenList;
    }
}

Related

  1. changeDateWithSplit(String argDate, String split)
  2. isValidDate(String sDate)
  3. getDateTimeFromString(String dateTimeString, String format)
  4. convertStringToDate(String date, String time)
  5. GetDate(String time)
  6. fromSlDateTime(final String dateString, final String timeString)
  7. convertTimeStumpToDate(String time)
  8. convertimeStumpToDate2(String time)
  9. convertDateTimeStrFormat(String dateStr, String pattern, String newPattern)