Here you can find the source of dateToString(String date, String format)
public static Date dateToString(String date, String format)
//package com.java2s; /*//from w w w . j av a 2 s.c o m * Copyright (C) 2015 The CloudKit Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static String dateToString(Date date) { if (date == null) { return ""; } return dateToString(date, "yyyy-MM-dd HH:mm:ss"); } public static Date dateToString(String date) { if (date == null) { return null; } return dateToString(date, "yyyy-MM-dd HH:mm:ss"); } public static Date dateToString(String date, String format) { if (notEmpty(date)) { SimpleDateFormat sdf = new SimpleDateFormat(format); try { return sdf.parse(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return new Date(); } else { return null; } } public static String dateToString(Date date, String format) { if (date != null) { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } else { return ""; } } public static boolean notEmpty(String s) { return s != null && !"".equals(s) && !"null".equals(s); } public static boolean notEmpty(Object[] arr) { return arr != null && arr.length > 0; } }