/**
 ***************************************************************************\
 * DateU -- handy Date formatting shortcuts
 *
 * Created by Mark Huss 
 *
 * THIS SOFTWARE IS NOT COPYRIGHTED
 *
 * This source code is offered for use in the public domain. You may
 * use, modify or distribute it freely.
 *
 * This code is distributed in the hope that it will be useful but
 * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
 * DISCLAMED. This includes but is not limited to warranties of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Darn lawyers.
 *
\***************************************************************************/

package com.mhuss.Util;

import java.util.Date;
import java.text.DateFormat;
import java.util.Calendar;

/**
 * A simple set of date and time formatters. This once trivial task
 * got a bit complicated after the addition of i18n support.
 */
public class DateU {
 /**
  * Return 'now' as a date String using the default Locale
  *
  * @param dateFmt The java.text.DateFormat constant to use
  *     (FULL, LONG, MEDIUM, or
  *     SHORT).
  *
  * @return The current, formatted, date as a String
  */
  public static String dateNow( int dateFmt ) {
    return DateFormat.getDateInstance( dateFmt ).format( new Date() );
  }

 /**
  * Return 'now' as a date String using the default Locale
  * and the DateFormat.MEDIUM size.
  *
  * @return The current formatted date as a String
  */
  public static String dateNow() {
    return dateNow( DateFormat.MEDIUM );
  }

 /**
  * Return 'now' as a date and time String using the default
  * Locale and the DateFormat.MEDIUM size.
  *
  * @return The current, formatted, date and time as a String
  */
  public static String dateTimeNow() {
    return DateFormat.getInstance().format( new Date() );
  }

 /**
  * Convert a Date into a date String using the
  * default Locale.
  *
  * @param d The java.util.Date to convert
  * @param dateFmt The java.text.DateFormat constant to use
  *     (FULL, LONG, MEDIUM, or
  *     SHORT).
  *
  * @return The date formatted as a String
  */
  public static String date( Date d, int dateFmt ) {
    return DateFormat.getDateInstance( dateFmt ).format( d );
  }

 /**
  * Convert a Date into a date String using the
  * default Locale and the DateFormat.MEDIUM size.
  *
  * @param d The java.util.Date to convert
  *
  * @return The date formatted as a String
  */
  public static String date( Date d ) {
    return date( d, DateFormat.MEDIUM );
  }

 /**
  * Convert a Date into a time String using the
  * default Locale
  *
  * @param d The java.util.Date to convert
  * @param dateFmt The java.text.DateFormat constant to use
  *     (FULL, LONG, MEDIUM, or
  *     SHORT).
  *
  * @return The time formatted as a String
  */
  public static String time( Date d, int dateFmt ) {
    return DateFormat.getTimeInstance( dateFmt ).format( d );
  }

 /**
  * Convert a Date into a time String using the
  * default Locale and the DateFormat.MEDIUM size.
  *
  * @param d The java.util.Date to convert
  *
  * @return The time formatted as a String
  */
  public static String time( Date d ) {
    return time( d, DateFormat.MEDIUM );
  }

 /**
  * Convert a Date into a date and time String using
  * the default Locale
  *
  * @param d The java.util.Date to convert
  * @param dtFmt The java.text.DateFormat constant to use
  *     (FULL, LONG, MEDIUM, or
  *     SHORT).
  *
  * @return The date and time formatted as a String
  */
  public static String dateTime( Date d, int dtFmt ) {
    return DateFormat.getDateTimeInstance( dtFmt, dtFmt ).format( d );
  }

 /**
  * Convert a Date into a date and time String using
  * the default Locale and the DateFormat.MEDIUM size.
  *
  * @param d The java.util.Date to convert
  *
  * @return The date and time formatted as a String
  */
  public static String dateTime( Date d ) {
    return dateTime( d, DateFormat.MEDIUM );
  }

 /**
  * Convert a Calendar into a date and time String
  * using the default Locale and the DateFormat.MEDIUM size.
  *
  * @param c The java.util.Calendar to convert
  *
  * @return The date and time formatted as a String
  */
  public static String dateTime( Calendar c ) {
    return dateTime( c.getTime(), DateFormat.MEDIUM );
  }
}

 

Ads help maintain this website.