本文整理汇总了Java中java.text.SimpleDateFormat.setLenient方法的典型用法代码示例。如果您正苦于以下问题:Java SimpleDateFormat.setLenient方法的具体用法?Java SimpleDateFormat.setLenient怎么用?Java SimpleDateFormat.setLenient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.text.SimpleDateFormat
的用法示例。
在下文中一共展示了SimpleDateFormat.setLenient方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testFormats
import java.text.SimpleDateFormat; //导入方法依赖的package包/类
/**
* Attempt to parse an input string with the allowed formats, returning
* null if none of the formats work.
*/
private static Date testFormats (String in, String[] formats)
throws ParseException
{
for (int i = 0; i <formats.length; i++)
{
try
{
SimpleDateFormat dateFormat = new SimpleDateFormat(formats[i]);
dateFormat.setLenient(false);
return dateFormat.parse(in);
}
catch (ParseException pe)
{
}
}
return null;
}
示例2: validateEndDate
import java.text.SimpleDateFormat; //导入方法依赖的package包/类
private static void validateEndDate(String startDate, String endDate) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setLenient(false);
Date batchEndDate = null;
Date batchStartDate = null;
try {
batchEndDate = format.parse(endDate);
batchStartDate = format.parse(startDate);
} catch (Exception e) {
throw new ProjectCommonException(ResponseCode.dateFormatError.getErrorCode(),
ResponseCode.dateFormatError.getErrorMessage(), ERROR_CODE);
}
if (batchStartDate.getTime() >= batchEndDate.getTime()) {
throw new ProjectCommonException(ResponseCode.endDateError.getErrorCode(),
ResponseCode.endDateError.getErrorMessage(), ERROR_CODE);
}
}
示例3: isValidDate
import java.text.SimpleDateFormat; //导入方法依赖的package包/类
/**
* Verify that the string is a valid date format.
*/
private boolean isValidDate(String date, String template) {
boolean convertSuccess = true;
// Specified date format
SimpleDateFormat format = new SimpleDateFormat(template, Locale.CHINA);
try {
// Set lenient to false., otherwise SimpleDateFormat will validate the date more loosely,
// for example, 2015/02/29 will be accepted and converted to 2015/03/01.
format.setLenient(false);
format.parse(date);
} catch (Exception e) {
// If throw, java.text.ParseException, or NullPointerException,
// it means the format is wrong.
convertSuccess = false;
}
return convertSuccess;
}
示例4: convertToDate
import java.text.SimpleDateFormat; //导入方法依赖的package包/类
public static Date convertToDate(String input) {
Date date = null;
if (null == input) {
return null;
}
for (SimpleDateFormat format : dateFormats) {
try {
format.setLenient(false);
date = format.parse(input);
}
catch (ParseException e) {
// Shhh.. try other formats
}
if (date != null) {
break;
}
}
return date;
}
示例5: parse
import java.text.SimpleDateFormat; //导入方法依赖的package包/类
/**
* 用指定的格式解析日期时间.
*
* @param date 时间字符串
* @param patterns 多个模式,see {@link java.text.SimpleDateFormat}
* @return 如果无法解析,那么返回 {@code null}
*/
public static Date parse(String date, String[] patterns) {
if (date == null || date.length() == 0) {
return null;
}
date = date.trim();
for (String pattern : patterns) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
df.setLenient(false);
try {
ParsePosition pp = new ParsePosition(0);
Date d = df.parse(date, pp);
if (d != null && pp.getIndex() == date.length()) {
return d;
}
} catch (Exception e) {
}
}
return null;
}
示例6: getNameOrAbbrev
import java.text.SimpleDateFormat; //导入方法依赖的package包/类
/**
* Get the full name or abbreviation of the month or day.
*/
private static String getNameOrAbbrev(String in,
String[] formatsIn,
String formatOut)
throws ParseException
{
for (int i = 0; i <formatsIn.length; i++) // from longest to shortest.
{
try
{
SimpleDateFormat dateFormat = new SimpleDateFormat(formatsIn[i], Locale.ENGLISH);
dateFormat.setLenient(false);
Date dt = dateFormat.parse(in);
dateFormat.applyPattern(formatOut);
return dateFormat.format(dt);
}
catch (ParseException pe)
{
}
}
return "";
}
示例7: dateFormatter
import java.text.SimpleDateFormat; //导入方法依赖的package包/类
/**
* Formats and converts a date value into string representation in the
* desired new date format
*
* @param inputValue
* the date value in old format
* @param oldFormat
* the date format of the date passed in {@code inputValue}
* @param newFormat
* the desired date format
* @return string representation of date in new date format
* <p>
* the method returns null if any parameter is null
* @throws ParseException
* if the date value passed in {@code inputValue} does not match
* the {@code oldFormat}
*/
public static String dateFormatter(String inputValue, String oldFormat, String newFormat) {
if (inputValue == null || oldFormat == null || newFormat == null)
return null;
String newDateString = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(oldFormat);
sdf.setLenient(false);
Date date = null;
date = sdf.parse(inputValue);
sdf.applyPattern(newFormat);
newDateString = sdf.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return newDateString;
}
示例8: parseIsoDate
import java.text.SimpleDateFormat; //导入方法依赖的package包/类
/**
* Convertir une date au format ISO-8601 en une date de la classe java.util.Date.
*
* @param sDate une date ISO dans une chaîne de caractères (ex: "2016-01-01")
* @return une date de la classe java.util.Date (ou null si erreur)
*/
public static Date parseIsoDate(String sDate) {
Date date;
String nDate = sDate.trim();
// format ISO seulement avec la date ou aussi l'heure
SimpleDateFormat ldf;
if (nDate.contains(":")) {
ldf = getLocaleFormat(ISO8601_FORMAT_STANDARD);
} else {
ldf = getLocaleFormat(ISO8601_FORMAT_SHORT);
}
// on teste finalement si la date spécifiée peut être convertie
ldf.setLenient(false);
try {
date = ldf.parse(nDate);
} catch (ParseException ex) {
date = null;
}
return date;
}
示例9: isPastCutoverDate
import java.text.SimpleDateFormat; //导入方法依赖的package包/类
private static boolean isPastCutoverDate(String s) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ROOT);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
format.setLenient(false);
long time = format.parse(s.trim()).getTime();
return System.currentTimeMillis() > time;
}
示例10: isValidDate
import java.text.SimpleDateFormat; //导入方法依赖的package包/类
/**
* 验证字符串是否是一个合法的日期格式
*/
private boolean isValidDate(String date, String template) {
boolean convertSuccess = true;
// 指定日期格式
SimpleDateFormat format = new SimpleDateFormat(template, Locale.CHINA);
try {
// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2015/02/29会被接受,并转换成2015/03/01
format.setLenient(false);
format.parse(date);
} catch (Exception e) {
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
convertSuccess = false;
}
return convertSuccess;
}
示例11: checkValidDate
import java.text.SimpleDateFormat; //导入方法依赖的package包/类
/**
* Checks if the given string is a valid date
* Credit to the users on stackoverflow:
* https://stackoverflow.com/questions/2149680/regex-date-format-validation-on-java/18252071#18252071
* @param date Formatted as "yyyy/MM/dd"
* @return <code>true</code> if a valid date string
*/
public static boolean checkValidDate(String date) {
if (date == null || !date.matches("\\d{4}/[01]\\d/[0-3]\\d"))
return false;
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
df.setLenient(false);
try {
df.parse(date);
return true;
} catch (ParseException ex) {
return false;
}
}
示例12: isValid
import java.text.SimpleDateFormat; //导入方法依赖的package包/类
@Override
public boolean isValid(String text) {
try {
SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
df.setLenient(false);
df.parse(text);
return true;
} catch (ParseException e) {
}
return false;
}
示例13: getTwitterDate
import java.text.SimpleDateFormat; //导入方法依赖的package包/类
/**
* Convert a Twitter formatted date to a Java format
* @param date The twitter date
* @return a {@link Date} instance of the twitter date
* @throws java.text.ParseException If we can't parse the date
*/
public Date getTwitterDate(String date) throws java.text.ParseException {
final String TWITTER="EEE MMM dd HH:mm:ss ZZZZZ yyyy";
SimpleDateFormat sf = new SimpleDateFormat(TWITTER,Locale.ENGLISH);
sf.setLenient(true);
return sf.parse(date);
}
示例14: parseDateWithLeniency
import java.text.SimpleDateFormat; //导入方法依赖的package包/类
/**
* <p>Parses a string representing a date by trying a variety of different parsers.</p>
*
* <p>The parse will try each parse pattern in turn.
* A parse is only deemed successful if it parses the whole of the input string.
* If no parse patterns match, a ParseException is thrown.</p>
*
* @param str the date to parse, not null
* @param parsePatterns the date format patterns to use, see SimpleDateFormat, not null
* @param lenient Specify whether or not date/time parsing is to be lenient.
* @return the parsed date
* @throws IllegalArgumentException if the date string or pattern array is null
* @throws ParseException if none of the date patterns were suitable
* @see java.util.Calender#isLenient()
*/
private static Date parseDateWithLeniency(String str, String[] parsePatterns,
boolean lenient) throws ParseException {
if (str == null || parsePatterns == null) {
throw new IllegalArgumentException("Date and Patterns must not be null");
}
SimpleDateFormat parser = new SimpleDateFormat();
parser.setLenient(lenient);
ParsePosition pos = new ParsePosition(0);
for (int i = 0; i < parsePatterns.length; i++) {
String pattern = parsePatterns[i];
// LANG-530 - need to make sure 'ZZ' output doesn't get passed to SimpleDateFormat
if (parsePatterns[i].endsWith("ZZ")) {
pattern = pattern.substring(0, pattern.length() - 1);
}
parser.applyPattern(pattern);
pos.setIndex(0);
String str2 = str;
// LANG-530 - need to make sure 'ZZ' output doesn't hit SimpleDateFormat as it will ParseException
if (parsePatterns[i].endsWith("ZZ")) {
int signIdx = indexOfSignChars(str2, 0);
while (signIdx >=0) {
str2 = reformatTimezone(str2, signIdx);
signIdx = indexOfSignChars(str2, ++signIdx);
}
}
Date date = parser.parse(str2, pos);
if (date != null && pos.getIndex() == str2.length()) {
return date;
}
}
throw new ParseException("Unable to parse the date: " + str, -1);
}
示例15: parseDate
import java.text.SimpleDateFormat; //导入方法依赖的package包/类
/**
* Convertit une chaîne de caractères (String) représentant une date en une date de la
* classe java.util.Date. Cette version accepte les années avec ou sans le siècle et
* également les dates sans l'année (cela prend alors l'année en cours).
*
* @param sDate la chaîne contenant une date
* @return une date de la classe java.util.Date (ou null si erreur)
*/
public static Date parseDate(String sDate) {
Date date = null;
String nDate = sDate.trim();
String info[] = getLocalePatternInfo();
String t[] = nDate.split(info[1]);
// s'il manque l'année, on prend l'année en cours
if (t.length == 2) {
nDate += info[0] + getYear(getNow());
t = nDate.split(info[1]);
}
// teste si on dispose des 3 parties de la date
if (t.length == 3) {
SimpleDateFormat ldf;
// si l'année fournie est composée de plus de 2 caractères, on change le format
if (t[2].length() == 2) {
ldf = getLocaleFormat(info[2]);
} else {
ldf = getLocaleFormat(info[2] + "yy");
}
// on teste finalement si la date spécifiée peut être convertie
ldf.setLenient(false);
try {
date = ldf.parse(nDate);
} catch (ParseException ex) {
date = null;
}
}
return date;
}