Java Calendar Time setDateTimeByString(Calendar theDateTime, String str)

Here you can find the source of setDateTimeByString(Calendar theDateTime, String str)

Description

set the calendar value of theDateTime according to a string like "2008-08-08T08:08:08"

License

Open Source License

Parameter

Parameter Description
theDateTime a parameter
str a parameter

Declaration

public static void setDateTimeByString(Calendar theDateTime, String str) 

Method Source Code

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

import java.util.Calendar;

public class Main {
    /**/*from www . j  a  va 2  s  .c o m*/
     * set the calendar value of theDateTime according to a string like "2008-08-08T08:08:08"
     * @param theDateTime
     * @param str
     */
    public static void setDateTimeByString(Calendar theDateTime, String str) {
        str = str.trim();
        String year, month, day;
        int i1, i2, iT;
        i1 = str.indexOf("-");
        i2 = str.indexOf("-", i1 + 1);
        iT = str.indexOf('T');
        year = str.substring(0, i1);
        month = str.substring(i1 + 1, i2);
        day = str.substring(i2 + 1, iT);
        i1 = str.indexOf(':', iT + 1);
        i2 = str.indexOf(':', i1 + 1);

        theDateTime.set(Calendar.MILLISECOND, 0);
        theDateTime.set(Calendar.SECOND,
                Integer.parseInt(str.substring(i2 + 1)));
        theDateTime.set(Calendar.MINUTE,
                Integer.parseInt(str.substring(i1 + 1, i2)));
        theDateTime.set(Calendar.HOUR_OF_DAY,
                Integer.parseInt(str.substring(iT + 1, i1)));
        theDateTime.set(Calendar.DAY_OF_MONTH, 1);
        theDateTime.set(Calendar.MONTH, Integer.parseInt(month, 10) - 1);
        theDateTime.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day, 10));
        theDateTime.set(Calendar.YEAR, Integer.parseInt(year, 10));
    }
}

Related

  1. resetTime(Calendar cal)
  2. resetTime(Calendar cal)
  3. resetTime(GregorianCalendar cal)
  4. roundTime(double dt, Calendar tmp)
  5. sameTime(Calendar one, Calendar two)
  6. setTime(Calendar cal, int h, int m, int s, int ms)
  7. setTime(Calendar cal, String time)
  8. setTime(final Calendar calendat, final String[] test)
  9. setTimeFrom(Calendar c)