Converts date to string using US locale, specified format. - Java java.util

Java examples for java.util:Date Format

Description

Converts date to string using US locale, specified format.

Demo Code


//package com.java2s;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.SimpleTimeZone;

public class Main {
    public final static String DEFAULT = "MM/dd/yyyy hh:mm:ss a Z";

    /**/*  w  w  w.  ja  v a 2  s.  c o m*/
     * Converts date to string using US locale, specified format. forces GMT timezone
     * @param format format string to use
     * @param date date to format
     * @return date formatted as string
     */
    public static String formatGMT(String format, Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
        sdf.setTimeZone(new SimpleTimeZone(0, "GMT"));

        return sdf.format(date);
    }

    /**
     * Converts date to string using US locale and specified format
     * @param format format string to use
     * @param date date to format
     * @return date formatted as string
     */
    public static String format(String format, Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
        return sdf.format(date);

    }

    public static String format(Date date) {
        return format(DEFAULT, date);
    }
}

Related Tutorials