Here you can find the source of convertStringToDate(String date)
public static Date convertStringToDate(String date)
//package com.java2s; /*//from ww w . j a v a 2s.c o m Unisens Library - library for a universal sensor data format Copyright (C) 2008 FZI Research Center for Information Technology, Germany Institute for Information Processing Technology (ITIV), KIT, Germany This file is part of the Unisens Library. For more information, see <http://www.unisens.org> The Unisens Library 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. The Unisens Library 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 the Unisens Library. If not, see <http://www.gnu.org/licenses/>. */ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Pattern; public class Main { public static Date convertStringToDate(String date) { try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); SimpleDateFormat simpleDateFormatWithMs = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); String pattern = "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}"; if (Pattern.matches(pattern, date)) { return simpleDateFormatWithMs.parse(date); } return simpleDateFormat.parse(date); } catch (ParseException pe) { pe.printStackTrace(); return null; } } }