Here you can find the source of parseToString(final Date date, final String format)
Parameter | Description |
---|---|
date | The Date to format to a String |
format | The Format for the date |
public static String parseToString(final Date date, final String format)
//package com.java2s; /**/*from w w w. j a v a 2 s . c o m*/ * Copyright (C) 2007 Asterios Raptis * * 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.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class Main { /** * The Method parseToString(Date, String) formats the given Date to the given Format. For * Example: Date date =new Date(System.currentTimeMillis()); String format = * "dd.MM.yyyy HH:mm:ss"; String now = UtilDate.parseToString(date, format); * System.out.println(now); The output would be something like this:'15.07.2005 14:12:00' * * @param date * The Date to format to a String * @param format * The Format for the date * @return The formated String */ public static String parseToString(final Date date, final String format) { return parseToString(date, format, Locale.getDefault(Locale.Category.FORMAT)); } /** * The Method parseToString(Date, String) formats the given Date to the given Format. For * Example: Date date =new Date(System.currentTimeMillis()); String format = * "dd.MM.yyyy HH:mm:ss"; String now = UtilDate.parseToString(date, format); * System.out.println(now); The output would be something like this:'15.07.2005 14:12:00' * * @param date * The Date to format to a String * @param format * The Format for the date * @param locale * The Locale object in which Language to get the format string. * @return The formated String */ public static String parseToString(final Date date, final String format, Locale locale) { final DateFormat df = new SimpleDateFormat(format, locale); final String result = df.format(date); return result; } /** * The Method parseToString(String, String, String) formats the given Date as string from the * current Format to the new given Format. For Example:<br> * <br> * <code> String expected = "20120810";<br> * String actual = ParseDateUtils.parseToString( * ParseDateUtils.parseToDate("10.08.2012", "dd.MM.yyyy"), "yyyyMMdd"); * </code><br> * <br> * The output is:20120810 * * @param date * The date as String object that shell be parsed to the new format * @param currentformat * The current format from the date * @param newFormat * The new format for the output date as String object * @return The formated String in the new date format * @throws ParseException * occurs when their are problems with parsing the String to Date. */ public static String parseToString(final String date, final String currentformat, String newFormat) throws ParseException { final Date currentDate = parseToDate(date, currentformat); final String result = parseToString(currentDate, newFormat); return result; } /** * Parses the String date to a date object. For example: USA-Format is : yyyy-MM-dd * * @param date * The Date as String * @param format * The Format for the Date to parse * @return The formated Date * @throws ParseException * occurs when their are problems with parsing the String to Date. */ public static Date parseToDate(final String date, final String format) throws ParseException { final DateFormat df = new SimpleDateFormat(format); Date parsedDate = null; parsedDate = df.parse(date); return parsedDate; } /** * Returns a date-object if the array with the formats are valid otherwise null. * * @param datum * The date as string which to parse to a date-object. * @param formats * The string-array with the date-patterns. * @param locale * THe Locale for the SimpleDateFormat. * @return A date-object if the array with the formats are valid otherwise null. */ public static Date parseToDate(final String datum, final String[] formats, final Locale locale) { for (String format : formats) { final SimpleDateFormat sdf = new SimpleDateFormat(format, locale); try { return sdf.parse(datum); } catch (final ParseException e) { // Do nothing... } } return null; } }