Java Date to String dateToString(Date date, String pattern)

Here you can find the source of dateToString(Date date, String pattern)

Description

date To String

License

Open Source License

Declaration

public static String dateToString(Date date, String pattern) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {

    public static String dateToString(Date date, String pattern) {
        if (date == null) {
            return "";
        } else {//from ww w .j  av a 2 s .  c  om
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            return format.format(date);
        }
    }

    public static String dateToString(int year, int month, int day, String pattern) {
        return dateToString(getDate(year, month, day), pattern);
    }

    public static Date getDate() {
        return Calendar.getInstance().getTime();
    }

    public static Date getDate(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month - 1, day, 0, 0, 0);
        return cal.getTime();

    }

    public static Date getDate(int year, int month, int date, int hour, int mintue, int second) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DATE, date);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, mintue);
        cal.set(Calendar.SECOND, second);
        return cal.getTime();
    }
}

Related

  1. dateToString(Date date, String format)
  2. dateToString(Date date, String formatStr)
  3. DateToString(Date date, String parttern)
  4. DateToString(Date date, String pattern)
  5. dateToString(Date date, String pattern)
  6. dateToString(Date date, String pattern)
  7. dateToString(Date date, String sPattern)
  8. dateToString(Date date, String strFormat)
  9. datetostring(Date dateInst)