本文整理匯總了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;
}