Java Calendar Time parseTime(Calendar calendar, String time_string)

Here you can find the source of parseTime(Calendar calendar, String time_string)

Description

parse Time

License

Open Source License

Declaration

public static long parseTime(Calendar calendar, String time_string) 

Method Source Code


//package com.java2s;
/*/*  w ww  .  j a v  a2 s  . c o  m*/
BeepBeep, an event stream processor
Copyright (C) 2008-2017 Sylvain Hall?
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Calendar;

public class Main {
    public static long parseTime(Calendar calendar, String time_string) {
        String[] parts = time_string.split("[/ :]");
        int day = Integer.parseInt(parts[0]);
        int month = stringToMonth(parts[1]);
        int year = Integer.parseInt(parts[2]);
        int hours = Integer.parseInt(parts[3]);
        int minutes = Integer.parseInt(parts[4]);
        int seconds = Integer.parseInt(parts[5]);
        calendar.set(year, month, day, hours, minutes, seconds);
        return calendar.getTime().getTime() / 1000;
    }

    public static int stringToMonth(String m) {
        if (m.compareTo("Jan") == 0)
            return 1;
        if (m.compareTo("Feb") == 0)
            return 2;
        if (m.compareTo("Mar") == 0)
            return 3;
        if (m.compareTo("Apr") == 0)
            return 4;
        if (m.compareTo("May") == 0)
            return 5;
        if (m.compareTo("Jun") == 0)
            return 6;
        if (m.compareTo("Jul") == 0)
            return 7;
        if (m.compareTo("Aug") == 0)
            return 8;
        if (m.compareTo("Sep") == 0)
            return 9;
        if (m.compareTo("Oct") == 0)
            return 10;
        if (m.compareTo("Nov") == 0)
            return 11;
        return 12;
    }
}

Related

  1. mergeDateAndTime(final Calendar date, final Calendar time)
  2. mergeDateTime(Calendar date, Calendar time)
  3. newCalendar(final long time)
  4. newCalendarInstance(TimeZone timeZone)
  5. normalizeLocalTime(final Calendar cal)
  6. parseTime(String time, Calendar calendar)
  7. removeTime(Calendar c)
  8. removeTime(Calendar cal)
  9. removeTime(Calendar calendar)