Java Timestamp Format formatDateWithCinderellaTime(Date date)

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

Description

Format a date in the following format 2007-09-27 23:59:00

License

Open Source License

Parameter

Parameter Description
passoutDate a parameter

Exception

Parameter Description
FordResourceException an exception

Return

Timestamp

Declaration

public static Timestamp formatDateWithCinderellaTime(Date date) 

Method Source Code


//package com.java2s;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    /**// w  w  w  .j  a  va 2 s . c  om
    * Format a date in the following format 
     * 2007-09-27 23:59:00
    * @param passoutDate
    * @return Timestamp
    * @throws FordResourceException
    */
    public static Timestamp formatDateWithCinderellaTime(Date date) {

        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(date);
        gregorianCalendar.set(GregorianCalendar.HOUR_OF_DAY, 23);
        gregorianCalendar.set(GregorianCalendar.MINUTE, 59);
        gregorianCalendar.set(GregorianCalendar.SECOND, 0);
        Date formattedPassoutDate = gregorianCalendar.getTime();

        return new Timestamp(formattedPassoutDate.getTime());
    }

    public static Timestamp formatDateWithCinderellaTime(String date) {
        try {
            return formatDateWithCinderellaTime(convertToDate(date));
        } catch (ParseException pe) {
            return null;
        }
    }

    /**
     * Convert a String into a date
     * @param   dateAsString. A String entered that is to be converted
     * to a string.
     * @return   The java.util.Date version of the string
     * @throws  ParseException if the date can not be converted     
     */
    public static Date convertToDate(String dateAsString) throws ParseException {
        Date utilDate = null;

        if (isDatePopulated(dateAsString)) {
            DateFormat df = DateFormat.getInstance();
            df.setLenient(false);
            SimpleDateFormat sf = (SimpleDateFormat) df;
            sf.applyPattern("dd/MM/yyyy");
            utilDate = sf.parse(dateAsString);
        }
        return utilDate;
    }

    /**
     * Determine whether an attribute is populated.
     * @param attribute
     * @return true if the attribute contains a value.
     */
    public static boolean isDatePopulated(String attribute) {
        boolean populated = true;
        if ((attribute == null) || (attribute.trim().length() == 0) || (attribute.equalsIgnoreCase("dd/mm/yy"))
                || (attribute.equalsIgnoreCase("dd/mm/yyyy"))) {
            populated = false;
        }
        return populated;
    }
}

Related

  1. formatDateTime(String dTime)
  2. formatDateTime(Timestamp t, String pattern)
  3. formatDateTimeStamp(final Date date)
  4. formatDateToTimestamp(String string)
  5. formatDateToUnixTimestamp(Date date)
  6. formatDefaultDate(Timestamp time)
  7. formatDuration(final long duration)
  8. formatElapsed(long elapsed)
  9. formatFileSize(Number data)