本文整理匯總了Java中java.text.SimpleDateFormat.applyPattern方法的典型用法代碼示例。如果您正苦於以下問題:Java SimpleDateFormat.applyPattern方法的具體用法?Java SimpleDateFormat.applyPattern怎麽用?Java SimpleDateFormat.applyPattern使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.text.SimpleDateFormat
的用法示例。
在下文中一共展示了SimpleDateFormat.applyPattern方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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 "";
}
示例2: getInstance
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
private DateFormat getInstance(int dateStyle, int timeStyle, Locale locale) {
if (locale == null) {
throw new NullPointerException();
}
SimpleDateFormat sdf = new SimpleDateFormat("", locale);
Calendar cal = sdf.getCalendar();
try {
String pattern = LocaleProviderAdapter.forType(type)
.getLocaleResources(locale).getDateTimePattern(timeStyle, dateStyle,
cal);
sdf.applyPattern(pattern);
} catch (MissingResourceException mre) {
// Specify the fallback pattern
sdf.applyPattern("M/d/yy h:mm a");
}
return sdf;
}
示例3: parse
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
protected static final Date parse(SimpleDateFormat parser, ParsePosition pos, String parsePattern, String txt) {
String pattern = parsePattern;
if (parsePattern.endsWith("ZZ")) {
pattern = pattern.substring(0, pattern.length() - 1);
}
parser.applyPattern(pattern);
pos.setIndex(0);
String str2 = txt;
if (parsePattern.endsWith("ZZ")) {
str2 = txt.replaceAll("([-+][0-9][0-9]):([0-9][0-9])$", "$1$2");
}
final Date date = parser.parse(str2, pos);
if (date != null && pos.getIndex() == str2.length()) {
return date;
}
return null;
}
示例4: 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;
}
示例5: parseDate
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
public static Date parseDate(String timestampStr, List<String> dateFormats, String timezone)
throws ParseException {
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat();
for (String s : dateFormats) {
sdf.applyPattern(s);
if (timezone != null && !timezone.isEmpty()) {
sdf.setTimeZone(TimeZone.getTimeZone(timezone));
}
try {
date = sdf.parse(timestampStr);
} catch (ParseException ignored) {
//do nothing
}
}
if (date == null) {
throw new ParseException(timestampStr, 0);
}
return date;
}
示例6: getDays
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
private List<Day> getDays(Elements tableHeaderCells) throws ParseException {
List<Day> days = new ArrayList<>();
for (int i = 2; i < 7; i++) {
String[] dayHeaderCell = tableHeaderCells.get(i).html().split("<br>");
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy", Locale.ROOT);
Date d = sdf.parse(dayHeaderCell[1].trim());
sdf.applyPattern("yyyy-MM-dd");
Day day = new Day();
day.setDayName(dayHeaderCell[0]);
day.setDate(sdf.format(d));
if (tableHeaderCells.get(i).hasClass("free-day")) {
day.setFreeDay(true);
day.setFreeDayName(dayHeaderCell[2]);
}
days.add(day);
}
return days;
}
示例7: downloadFromNoaa
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
/**
*
* @param date date of the shape file to download
* @param outputFolder ouput folder destination
* @return
*/
public File downloadFromNoaa(Date date,String outputFolder){
SimpleDateFormat df=new SimpleDateFormat();
df.applyPattern(TOKEN_DATE);
String fileName=noaaFileName.replace(TOKEN_DATE,df.format(date));
df.applyPattern(TOKEN_YEAR);
String strurl=noaaBaseUrl.replace(TOKEN_YEAR,df.format(date))+fileName;
return download(strurl,outputFolder+File.separator+fileName);
}
示例8: downloadFromMasie
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
/**
*
* @param date date of the shape file to download
* @param outputFolder ouput folder destination
* @return
*/
public File downloadFromMasie(Date date,String outputFolder){
SimpleDateFormat df=new SimpleDateFormat();
df.applyPattern(TOKEN_DATE);
String fileName=masieFileName.replace(TOKEN_DATE,df.format(date));
df.applyPattern(TOKEN_YEAR);
String strurl=masieBaseUrl.replace(TOKEN_YEAR,df.format(date))+fileName;
return download(strurl,outputFolder+File.separator+fileName);
}
示例9: showInitialReleaseDate
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
private void showInitialReleaseDate(Date date) {
String pattern = "dd MMM yyyy";
SimpleDateFormat dateFormat = new SimpleDateFormat();
try {
dateFormat.applyPattern(pattern);
String formattedDate = dateFormat.format(date);
mInitialReleaseDate.setText(formattedDate);
} catch (Exception e) {
e.printStackTrace();
}
}
示例10: FormatDate
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
/**
* Converts and formats an instant into a string of date with the specified pattern.
*
* @see SimpleDateFormat
*
* @param date date to format
* @param pattern format of the date e.g. MM/DD/YYYY or MMM d, yyyy
* @return formatted date
*/
@SimpleFunction
public static String FormatDate(Calendar date, String pattern) {
SimpleDateFormat formatdate = new SimpleDateFormat();
if (pattern.length() == 0) {
formatdate.applyPattern("MMM d, yyyy");
} else {
formatdate.applyPattern(pattern);
}
return formatdate.format(date.getTime());
}
示例11: 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);
}
示例12: createDate
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
private static String createDate(Date date, String defaultFormat, Boolean utc) {
SimpleDateFormat gmtFormat = new SimpleDateFormat();
gmtFormat.applyPattern(defaultFormat);
TimeZone gmtTime = (utc) ? TimeZone.getTimeZone("GMT") : TimeZone.getDefault();
gmtFormat.setTimeZone(gmtTime);
return gmtFormat.format(date);
}
示例13: getTimeInterval
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
/**
* 返回距離現在的時間間隔
* 時間格式:yyyy-MM-ddTHH:mm:ss.SSSZ
* @param formatTime
* @return
*/
public static String getTimeInterval(String formatTime) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
simpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss.SSS");
String time = formatTime.replace("T", " ").replace("Z", "");
Date date = null;
try {
date = simpleDateFormat.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
long seconds = (System.currentTimeMillis() - date.getTime()) / 1000;
if (seconds < 60) {
return "剛剛";
}
seconds /= 60;
if (seconds < 60) {
return seconds + "分鍾前";
}
seconds /= 60;
if (seconds < 24) {
return seconds + "小時前";
}
seconds /= 24;
if (seconds < 30) {
return seconds + "小時前";
}
seconds /= 30;
if (seconds < 12) {
return seconds + "月前";
}
seconds /= 12;
return seconds + "年前";
}
示例14: preprocessText
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
private String[] preprocessText() {
String[] text;
if (this.text == null || this.text.length == 0 || (this.text.length == 1 && TextUtils.isEmpty(this.text[0]) == true)) {
text = defaultText;
} else {
text = new String[this.text.length];
for (int i = 0; i < text.length; i++) {
text[i] = new String(this.text[i] != null ? this.text[i] : "");
Date date = Calendar.getInstance().getTime();
SimpleDateFormat sdf = new SimpleDateFormat();
for (int j = 0; j < DATE_REPLACEMENTS.length; j++) {
while (text[i].indexOf(DATE_REPLACEMENTS[j]) >= 0) {
sdf.applyPattern(DATE_FORMAT[j]);
text[i] = text[i].replace(DATE_REPLACEMENTS[j], sdf.format(date));
}
}
}
}
if (text == null) {
if (defaultText == null) {
text = new String[]{""};
} else {
text = defaultText;
}
}
return text;
}
示例15: getPartString
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
public String getPartString(Session session, Object dateTime, int part) {
String javaPattern = "";
switch (part) {
case DAY_NAME :
javaPattern = "EEEE";
break;
case MONTH_NAME :
javaPattern = "MMMM";
break;
}
SimpleDateFormat format = session.getSimpleDateFormatGMT();
try {
format.applyPattern(javaPattern);
} catch (Exception e) {}
Date date = (Date) convertSQLToJavaGMT(session, dateTime);
return format.format(date);
}