Java Timestamp Create getTimeStampFromISOString(String isoString)

Here you can find the source of getTimeStampFromISOString(String isoString)

Description

get Time Stamp From ISO String

License

Open Source License

Declaration

public static Timestamp getTimeStampFromISOString(String isoString) 

Method Source Code

//package com.java2s;
/**/* ww  w .j  a v a2  s .  co m*/
 * @author David Garratt
 * 
 * Project Name : Commander4j
 * 
 * Filename     : JUtility.java
 * 
 * Package Name : com.commander4j.util
 * 
 * License      : GNU General Public License
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * http://www.commander4j.com/website/license.html.
 * 
 */

import java.sql.Timestamp;

import java.util.Calendar;

public class Main {
    public static Timestamp getTimeStampFromISOString(String isoString) {
        Timestamp result;

        // 2010-05-31T10:14:49
        // 0123456789111111111
        // 012345678
        try {
            Calendar caldate = Calendar.getInstance();

            int year = 0;
            int month = 0;
            int day = 0;
            int hour = 0;
            int min = 0;
            int second = 0;

            year = Integer.valueOf(isoString.substring(0, 4));
            month = Integer.valueOf(isoString.substring(5, 7));
            day = Integer.valueOf(isoString.substring(8, 10));
            hour = Integer.valueOf(isoString.substring(11, 13));
            min = Integer.valueOf(isoString.substring(14, 16));
            second = Integer.valueOf(isoString.substring(17, 19));

            if ((month < 1) | (month > 12)) {
                throw new Exception("Invalid month " + String.valueOf(month));
            }

            if ((day < 1) | (day > 31)) {
                throw new Exception("Invalid day " + String.valueOf(day));
            }

            caldate.set(year, month - 1, day, hour, min, second);

            result = new Timestamp(caldate.getTimeInMillis());

            result.setNanos(0); // or other value
        } catch (Exception ex) {
            result = null;
        }

        return result;
    }
}

Related

  1. getTimestampFormat()
  2. getTimestampFormatter()
  3. getTimestampFromDate(Date d)
  4. getTimestampFromDate(String d)
  5. getTimestampFromDateTime(DateTime dateTime)
  6. getTimestampFromString(String dateStr)
  7. getTimestampFromString(String dateString, String dateFormat)
  8. getTimestampFromString(String id)
  9. getTimestampFromString(String s)