Here you can find the source of getDateTime(String s)
public static java.sql.Date getDateTime(String s) throws IllegalArgumentException
//package com.java2s; /*//from w ww .j a va2 s . c o m * aocode-public - Reusable Java library of general tools with minimal external dependencies. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2013, 2016, 2018 AO Industries, Inc. * support@aoindustries.com * 7262 Bull Pen Cir * Mobile, AL 36695 * * This file is part of aocode-public. * * aocode-public 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. * * aocode-public 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 aocode-public. If not, see <http://www.gnu.org/licenses/>. */ import java.sql.Timestamp; import java.util.Calendar; public class Main { /** * Gets the date and time from the YYYY-MM-DD HH:MM:SS format. */ public static java.sql.Date getDateTime(String s) throws IllegalArgumentException { if (s.length() != 19) throw new IllegalArgumentException("Invalid date: " + s); Calendar cal = Calendar.getInstance(); int year = Integer.parseInt(s.substring(0, 4)); cal.set(Calendar.YEAR, year); int month = Integer.parseInt(s.substring(5, 7)); if (month < 1 || month > 12) throw new IllegalArgumentException("Invalid date: " + s); cal.set(Calendar.MONTH, month - 1); int day = Integer.parseInt(s.substring(8, 10)); if (day < 1 || day > cal.getActualMaximum(Calendar.DATE)) throw new IllegalArgumentException("Invalid date: " + s); cal.set(Calendar.DATE, day); int hour = Integer.parseInt(s.substring(11, 13)); if (hour < 0 || hour > 23) throw new IllegalArgumentException("Invalid hour: " + hour); cal.set(Calendar.HOUR_OF_DAY, hour); int minute = Integer.parseInt(s.substring(14, 16)); if (minute < 0 || minute > 59) throw new IllegalArgumentException("Invalid minute: " + minute); cal.set(Calendar.MINUTE, minute); int second = Integer.parseInt(s.substring(17, 19)); if (second < 0 || second > 59) throw new IllegalArgumentException("Invalid second: " + second); cal.set(Calendar.SECOND, second); cal.set(Calendar.MILLISECOND, 0); return new java.sql.Date(cal.getTimeInMillis()); } /** * Gets the database format for a date/time field. * * @param allowNull When {@code true}, if the time is {@code -1}, returns {@code null} */ public static String getDateTime(long time, boolean allowNull) { return allowNull && time == -1 ? null : new Timestamp(time).toString().substring(0, 19); // TODO: Not y10k compliant } /** * Gets the database format for a date/time field. * * @param time if the time is {@code -1}, returns {@code null} * * @see #getDateTime(long, boolean) * * @deprecated Please use {@link #getDateTime(long, boolean)} to specify if {@code null} is allowed */ @Deprecated public static String getDateTime(long time) { return getDateTime(time, true); } }