Java Calendar Time calendarFromString(String dateString, String timeString)

Here you can find the source of calendarFromString(String dateString, String timeString)

Description

Returns calendar instance created from given date string.

License

Open Source License

Parameter

Parameter Description
dateString a parameter
timeString a parameter

Declaration

public static Calendar calendarFromString(String dateString, String timeString) 

Method Source Code

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

import java.util.Calendar;

import java.util.GregorianCalendar;

public class Main {
    /**/*from  ww  w . jav a  2 s  .c  o m*/
     * Returns calendar instance created from given date string. WARNING : sets
     * millisecond field to 0 automatically !
     * 
     * @param dateString
     * @param timeString
     * @return
     */
    public static Calendar calendarFromString(String dateString, String timeString) {
        if (dateString.length() < 10)
            return null;
        if (timeString.length() < 8)
            return null;
        GregorianCalendar cal = new GregorianCalendar();
        try {
            int day = Integer.parseInt(dateString.substring(0, 2));
            int month = Integer.parseInt(dateString.substring(3, 5));
            int year = Integer.parseInt(dateString.substring(6, 10));
            int hour = Integer.parseInt(timeString.substring(0, 2));
            int minute = Integer.parseInt(timeString.substring(3, 5));
            int second = Integer.parseInt(timeString.substring(6, 8));

            cal.set(Calendar.DAY_OF_MONTH, day);
            // 0=ocak, 1=subat tir
            cal.set(Calendar.MONTH, month - 1);
            cal.set(Calendar.YEAR, year);
            // 24 saat seklindeki saat set ediliyor
            cal.set(Calendar.HOUR_OF_DAY, hour);
            cal.set(Calendar.MINUTE, minute);
            cal.set(Calendar.SECOND, second);
            cal.set(Calendar.MILLISECOND, 0);

        } catch (Exception e) {
            return null;
        }
        return cal;
    }
}

Related

  1. calendar2TimeStamp(Calendar cal)
  2. calendarToGPSTime(Calendar calendar)
  3. ceilTime(Calendar calendar)
  4. clearCalendarTime(Calendar cal)
  5. clearCalendarTimeParts(Calendar cal)