Java String to Date toDateString(java.util.Date date, String spe)

Here you can find the source of toDateString(java.util.Date date, String spe)

Description

to Date String

License

Open Source License

Declaration

public static String toDateString(java.util.Date date, String spe) 

Method Source Code


//package com.java2s;
import java.util.Calendar;

import java.text.SimpleDateFormat;

public class Main {

    public static String toDateString(java.util.Date date, String spe) {
        if (date == null)
            return "";

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);/*from   w w  w  .  ja v  a 2 s. c o  m*/
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int year = calendar.get(Calendar.YEAR);

        String monthStr = "" + month;
        String dayStr = "" + day;
        String yearStr = "" + year;

        if (month < 10)
            monthStr = "0" + month;

        if (day < 10)
            dayStr = "0" + day;

        return yearStr + spe + monthStr + spe + dayStr;
    }

    public static String toDateString(java.util.Date date) {
        return toDateString(date, "-");
    }

    public static String toDateString(Calendar calendar) {
        return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    }
}

Related

  1. toDateString(Date date, TimeZone timezone)
  2. toDateString(final Date date)
  3. toDateString(java.util.Date date)
  4. toDateString(java.util.Date date)
  5. toDateString(java.util.Date date, String format)
  6. toDateString(long lval)
  7. toDateString(long ms)
  8. toDateString(String fmt, Date date)
  9. toDateStyleBoth(String dateStr)