Java Time Format getFormattedDateTime(Date date, String pattern)

Here you can find the source of getFormattedDateTime(Date date, String pattern)

Description

get Formatted Date Time

License

Open Source License

Declaration

public static String getFormattedDateTime(Date date, String pattern) 

Method Source Code


//package com.java2s;
/*//from   ww  w.  ja v  a 2 s .  c om
* Copyright (c) 2015, Anshoo Arora (Relevant Codes).  All rights reserved.
* 
* Copyrights licensed under the New BSD License.
* 
* See the accompanying LICENSE file for terms.
*/

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static String getFormattedDateTime(Date date, String pattern) {
        SimpleDateFormat sdfDate = new SimpleDateFormat(pattern);

        return sdfDate.format(date);
    }

    public static String getFormattedDateTime(String dateTime, String pattern) {
        SimpleDateFormat sdfDate = new SimpleDateFormat(pattern);
        DateFormat format = new SimpleDateFormat(pattern, Locale.ENGLISH);
        Date date;

        try {
            date = format.parse(dateTime);
            return sdfDate.format(date);
        } catch (ParseException e) {
            return dateTime;
        }
    }

    public static String getFormattedDateTime(long millis, String pattern) {
        SimpleDateFormat sdfDate = new SimpleDateFormat(pattern);
        Date date = new Date(millis);

        return sdfDate.format(date);
    }
}

Related

  1. getFormatDateTime(Date date, String format)
  2. getFormatDateTime(java.util.Date date)
  3. getFormattedCurrentDateAndTime()
  4. getFormattedCurrentDateTime(String pattern)
  5. getFormattedDateAndTime(final Date date)
  6. getFormattedDateTime(String date, String dateFormat)
  7. getFormattedDuration(long fromTime)
  8. getFormattedFileTime(File f, String format)
  9. getFormattedTime()