Example usage for java.text SimpleDateFormat setLenient

List of usage examples for java.text SimpleDateFormat setLenient

Introduction

In this page you can find the example usage for java.text SimpleDateFormat setLenient.

Prototype

public void setLenient(boolean lenient) 

Source Link

Document

Specify whether or not date/time parsing is to be lenient.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
    // Prints a value in 2014
    System.out.println(format.parse("16-16-2013"));
    format.setLenient(false);
    // Throws an exception
    System.out.println(format.parse("16-16-2013"));
}

From source file:Main.java

public static void main(String[] argv) {
    java.util.Date date;//from   ww w.j  a va  2  s.co  m
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    // Lenient
    try {
        date = sdf.parse("40/02/2015");
        System.out.println("Lenient date is :                  " + date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    // Rigorous
    sdf.setLenient(false);

    try {
        date = sdf.parse("40/02/2015");
        System.out.println("Rigorous date (won't be printed!): " + date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static boolean isValidDate(String inDate) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);
    try {//from   w  w  w  . ja  v  a 2  s .  co m
        dateFormat.parse(inDate.trim());
    } catch (ParseException pe) {
        return false;
    }
    return true;
}

From source file:Main.java

public static String getDateTimeString(Calendar cal) {
    SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
    df.setLenient(false);
    String s = df.format(cal.getTime());
    return s;/* www. ja v a 2s .c  o  m*/
}

From source file:Main.java

public static Date getDateFromString(String date) {
    SimpleDateFormat sf = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy");
    sf.setLenient(true);
    Date ret = new Date();
    try {/*  w  w  w .  j  a v  a  2s  . c o m*/
        ret = sf.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:Main.java

@Nullable
public static Date parseDate(@NonNull String format, @Nullable String date) {
    if (date == null) {
        return null;
    }/*from  w ww. j  av a2s. c o  m*/
    final SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
    formatter.setLenient(false);
    try {
        return formatter.parse(date);
    } catch (ParseException e) {
        return null;
    }
}

From source file:Main.java

public static boolean validateDate(String date) {
    SimpleDateFormat yearDf = new SimpleDateFormat("yyyy");
    yearDf.setLenient(false);
    SimpleDateFormat yearMonthDf = new SimpleDateFormat("yyyy-MM");
    yearMonthDf.setLenient(false);//  ww  w  . ja  va  2 s  .  c  o m
    SimpleDateFormat yearMonthDayDf = new SimpleDateFormat("yyyy-MM-dd");
    yearMonthDayDf.setLenient(false);

    try {
        if (date != null && date.trim().length() == 4) {
            Date result = yearDf.parse(date);
            return true;
        }

        else if (date.trim().length() == 7) {
            Date result = yearMonthDf.parse(date);
            return true;
        }

        else if (date.trim().length() == 10) {
            Date result = yearMonthDayDf.parse(date);
            return true;
        } else {
            return false;
        }

    } catch (ParseException e) {
        return false;
    }

}

From source file:Main.java

public static String calculateAge(Context context, String date) {
    final String FACEBOOK = "MM/dd/yyyy";
    try {//from   w  w  w.  j  a  v a  2s. c om
        SimpleDateFormat sf = new SimpleDateFormat(FACEBOOK, Locale.ENGLISH);
        sf.setLenient(true);
        Date birthDate = sf.parse(date);
        Date now = new Date();
        int age = now.getYear() - birthDate.getYear();
        birthDate.setHours(0);
        birthDate.setMinutes(0);
        birthDate.setSeconds(0);
        birthDate.setYear(now.getYear());
        if (birthDate.after(now)) {
            age -= 1;
        }
        return Integer.toString(age);

    } catch (Exception e) {
        Log.d("DEBUG", "exception in getTwitterDate:");
        e.printStackTrace();
        return "Unknown";
    }
}

From source file:Main.java

public static String getRelativeTimeAgo(String rawJsonDate) {
    String twitterFormat = "EEE MMM dd HH:mm:ss ZZZZZ yyyy";
    SimpleDateFormat sf = new SimpleDateFormat(twitterFormat, Locale.ENGLISH);
    sf.setLenient(true);

    String relativeDate = "";
    try {/*ww  w .  j  a va  2 s  . c om*/
        long dateMillis = sf.parse(rawJsonDate).getTime();
        relativeDate = DateUtils
                .getRelativeTimeSpanString(dateMillis, System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS)
                .toString();
    } catch (ParseException e) {
        e.printStackTrace();
    }

    // Shorten relative date
    relativeDate = relativeDate.replaceAll(" ", "").replace("seconds", "s").replace("second", "s")
            .replace("minutes", "m").replace("minute", "m").replace("hours", "h").replace("hour", "h")
            .replace("days", "d").replace("day", "d").replace("weeks", "w").replace("week", "w")
            .replace("months", "M").replace("month", "M").replace("years", "y").replace("year", "y")
            .replace("ago", "").replace("in", "").replace("0s", "just now");

    return relativeDate;
}

From source file:org.apt.demo.util.Tools.java

public static Timestamp convertString2Timestamp(String s) {
    Timestamp ts = null;//from   w ww  . j a  va2  s  .c  o  m
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        sdf.setLenient(false);
        Date d = sdf.parse(s);

        ts = new Timestamp(d.getTime());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return ts;
}