Android Date to String Convert dateToIsoDateString(@Nullable Date date, @NotNull DateFallback fallback)

Here you can find the source of dateToIsoDateString(@Nullable Date date, @NotNull DateFallback fallback)

Description

Converts a java.util.Date object to an ISO-formatted String representation of it.

Parameter

Parameter Description
date the java.util.Date to format. Can be null .
fallback if date is null , this parameter decides the return value.

Exception

Parameter Description
IllegalArgumentException - if date is null and fallback is invalid

Return

a formatted of the form "yyyy-MM-dd HH:mm"

Declaration

@NotNull
public static String dateToIsoDateString(@Nullable Date date,
        @NotNull DateFallback fallback) throws IllegalArgumentException 

Method Source Code

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main{
    /**//from  w w  w  .j av  a2 s. co m
     * Converts a {@link java.util.Date} object to an ISO-formatted {@link String} representation of it.
     *
     * @param date      the {@link java.util.Date} to format. Can be {@code null}.
     * @param fallback  if {@code date} is {@code null}, this parameter decides the return value.
     * @return          a formatted {@link String} of the form "yyyy-MM-dd HH:mm"
     * @throws          IllegalArgumentException - if {@code date} is {@code null} and
     *                  {@code fallback} is invalid
     * @see             #dateToIsoDateString(java.util.Date)
     * @see             #isoDateStringToDate(String)
     */
    @NotNull
    public static String dateToIsoDateString(@Nullable Date date,
            @NotNull DateFallback fallback) throws IllegalArgumentException {
        if (date != null) {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
        } else if (fallback == DateFallback.EARLIEST) {
            return getEarliestDateString();
        }
        throw new IllegalArgumentException(
                "date is null and fallback parameter is invalid!");
    }
    /**
     * Converts a {@link Date} object to an ISO-formatted {@link String} representation of it.
     *
     * @param date  the {@link Date} to format. Must not be {@code null}.
     * @return      a formatted {@link String} of the form "yyyy-MM-dd HH:mm"
     * @see         #dateToIsoDateString(Date, DateFallback)
     * @see         #isoDateStringToDate(String)
     */
    public static String dateToIsoDateString(@NotNull Date date) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
    }
    /**
     * Earliest possible date used in the app.
     */
    @NotNull
    public static String getEarliestDateString() {
        return "1970-01-01 00:00";
    }
}

Related

  1. convertToDateStamp(Date date)
  2. dateTimeToString(Date date)
  3. dateToIsoDateString(@NotNull Date date)
  4. formatDate(Date date, String format)
  5. formatDate(String format, Date date)
  6. formatDate(final Date date)
  7. getCurrentDateStr()