Java Hour Format FormatDateTime(String dateString)

Here you can find the source of FormatDateTime(String dateString)

Description

Formats the regular date string into a more humanly readable one

License

Open Source License

Parameter

Parameter Description
dateString the Date string in the format yyyy-MM-dd HH:mm:ss.S or yyyy-MM-ddTHH:mm:ssZ

Return

A date String in the format MMMM dd, yyyy, h:mm:ss.s a Or the original if it fails to parse it

Declaration

public static String FormatDateTime(String dateString) 

Method Source Code


//package com.java2s;
/*//from  w  ww.  j a va2s  .c om
 fEMR - fast Electronic Medical Records
 Copyright (C) 2014  Team fEMR
    
 fEMR is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 fEMR 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 General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with fEMR.  If not, see <http://www.gnu.org/licenses/>. If
 you have any questions, contact <info@teamfemr.org>.
*/

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

public class Main {
    /**
     * Formats the regular date string into a more humanly readable one
     *
     * @param dateString the Date string in the format yyyy-MM-dd HH:mm:ss.S or yyyy-MM-ddTHH:mm:ssZ
     * @return A date String in the format MMMM dd, yyyy, h:mm:ss.s a Or the original if it fails to parse it
     */
    public static String FormatDateTime(String dateString) {

        Date date;
        String formattedDate = dateString;
        String pattern = (dateString.indexOf('T') == -1) ? "yyyy-MM-dd HH:mm:ss.S" : "yyyy-MM-dd'T'HH:mm:ss";
        try {

            date = new SimpleDateFormat(pattern).parse(dateString);
            formattedDate = new SimpleDateFormat("MMMM dd, yyyy, h:mm:ss a").format(date);
        } catch (ParseException e) {

            e.printStackTrace();
        }

        return formattedDate;
    }
}

Related

  1. formatDateTime(java.util.Date d)
  2. formatDateTime(java.util.Date date)
  3. formatDateTime(java.util.Date date)
  4. formatDateTime(java.util.Date paramDate)
  5. formatDateTime(String date)
  6. formatDateTime(String dateTime)
  7. formatDateTime(String strDateTime, String strFormat)
  8. formatDateTime12(String dateStr)
  9. formatDateTime24()