Java Date to Time to24HrTimeString(Date date)

Here you can find the source of to24HrTimeString(Date date)

Description

Return the formatted string of the given date instance based on the 24 hour format specified for time in the System property.

License

Open Source License

Parameter

Parameter Description
date The Date instance.

Return

The Time String from the date instance in 24 hour format.

Declaration

public static String to24HrTimeString(Date date) 

Method Source Code

//package com.java2s;
/*//from  w w  w.  j a  v a2  s.  c  o  m
 * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF
 * Institute of Systems Science, National University of Singapore
 *
 * Copyright 2012 Team 4(Part-Time), ISS, NUS, Singapore. All rights reserved.
 * Use of this source code is subjected to the terms of the applicable license
 * agreement.
 *
 * -----------------------------------------------------------------
 * REVISION HISTORY
 * -----------------------------------------------------------------
 * DATE             AUTHOR          REVISION      DESCRIPTION
 * 10 March 2012    Chen Changfeng   0.1            Class creating
 *                                        
 *                                        
 *                                        
 *                                        
 * 
 */

import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    private static String time24HrFormat = "HH:mm";

    /**
     * Return the formatted string of the given date instance based on
     * the 24 hour format specified for time in the System property.
     * <p>
     * If the properties file is not available or the timeformat property has
     * not been specified, the default format "HH:mm" will be used.
     * <p>
     * @param  date The Date instance.
     * @return The Time String from the date instance in 24 hour format.
     */
    public static String to24HrTimeString(Date date) {

        if (date == null)
            return null;

        String timeformat = null;
        // Use the default format of "hh:mm aa".
        timeformat = time24HrFormat;

        return format(date, timeformat);
    }

    /**
     * Get the formatted string of the given date instance based on the
     * date format provided.
     * <p>
     * @param  date   The date that needs to be formatted.
     * @param  format The format to be applied to the date.
     * @return The formatted Date String.
     */
    public static String format(Date date, String format) {
        if (date == null || format == null)
            return null;

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(date);
    }
}

Related

  1. getXsdDateTime(final Date d)
  2. removeTime(Date date)
  3. serializeDateTime(final Date date)
  4. serializeXsdDateTime(Date date)
  5. strTime(Date dt)
  6. toExtendedTime(Date date)
  7. toGeneralizedTime(Date date)
  8. toLocalDateTime(Date date)
  9. toLocalTimeString(Date d)