本文整理匯總了Java中java.text.SimpleDateFormat.parse方法的典型用法代碼示例。如果您正苦於以下問題:Java SimpleDateFormat.parse方法的具體用法?Java SimpleDateFormat.parse怎麽用?Java SimpleDateFormat.parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.text.SimpleDateFormat
的用法示例。
在下文中一共展示了SimpleDateFormat.parse方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getWeekByYear
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
public static int getWeekByYear(String dateStr)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try
{
date = sdf.parse(dateStr);
}
catch (ParseException e)
{
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int i = calendar.get(Calendar.WEEK_OF_YEAR);
return i;
}
示例2: 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;
}
示例3: str2Date
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
public static Date str2Date(String str, String format) {
if (str == null || str.length() == 0) {
return null;
}
if (format == null || format.length() == 0) {
format = "yyyy-MM-dd HH:mm:ss";
}
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
date = sdf.parse(str);
} catch (Exception e) {
Log.e("TimeUtil",e.toString());
}
return date;
}
示例4: parse
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
public static Date parse(String date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return format.parse(date);
} catch (ParseException e) {
return null;
}
}
示例5: getNowTime
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
public static Date getNowTime() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String dateStr = dateFormat(date);
try {
date = format.parse(dateStr);
}
catch (ParseException e) {
e.printStackTrace();
}
return date;
}
示例6: testGoogleJwtCreatorExpTimeHasPassed
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
@Test
public void testGoogleJwtCreatorExpTimeHasPassed() throws Exception {
thrown.expect(TokenExpiredException.class);
thrown.expectMessage("The Token has expired on Wed Oct 29 00:00:00 PDT 2014.");
String myDate = "2014/10/29";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date date = sdf.parse(myDate);
long expLong = date.getTime();
Date expDate = new Date(expLong);
Algorithm algorithm = Algorithm.HMAC256("secret");
String token = GoogleJwtCreator.build()
.withPicture(PICTURE)
.withEmail(EMAIL)
.withIssuer("issuer")
.withSubject("subject")
.withAudience("audience")
.withExp(expDate)
.withIat(iat)
.withName(NAME)
.sign(algorithm);
GoogleVerification verification = GoogleJWT.require(algorithm);
JWT verifier = verification.createVerifierForGoogle(PICTURE, EMAIL, asList("issuer"), asList("audience"),
NAME, 1, 1).build();
DecodedJWT jwt = verifier.decode(token);
}
示例7: parseDate
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
static public Date parseDate(String dateString) {
Date date = null;
String[] formats = new String[]{DiaryDataSource.ISO_8601, DiaryDataSource.ISO_8601_SHORT};
for (String format : formats) {
SimpleDateFormat f = new SimpleDateFormat(format);
try {
date = f.parse(dateString);
return date;
} catch (ParseException ignored) {
}
}
Calendar calendar = Calendar.getInstance();
calendar.set(0, Calendar.JANUARY, 0);
return calendar.getTime();
}
示例8: formatDate
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
@SuppressLint("SimpleDateFormat")
public static String formatDate(String date) {
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat ft2 = new SimpleDateFormat("yyyy年MM月dd日");
try {
Date sDate = ft.parse(date);
return ft2.format(sDate).toString();
} catch (ParseException e) {
return "";
}
}
示例9: parseDateTime
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
public static Date parseDateTime(String dateTime) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
try {
return sdf.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
示例10: StrToDate
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
/**
* 字符串轉換成日期
*
* @param str
* @return date
*/
public static Date StrToDate(String str) {
SimpleDateFormat format = new SimpleDateFormat(dateFormatter);
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
示例11: parseDate
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
/**
* 獲得當前格式化以後的時間(yyyy-MM-dd HH:mm:ss)
*
* @param str
* @param pattern
* @return
*/
public static Date parseDate(String str, String pattern) {
if (StringUtils.isEmpty(str)) {
return null;
}
SimpleDateFormat format = new SimpleDateFormat(pattern);
try {
return format.parse(str);
} catch (Exception e) {
throw new RuntimeException("Could not parse date: "
+ e.getMessage(), e);
}
}
示例12: str2Date
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
/**
* 時間串轉化為Date
*
* @param dateStr
* dateFormat時間格式的字符串
* @param dateFormat
* 時間格式
* @return Date
*/
public static Date str2Date(String dateStr, String dateFormat) {
if (ValidateHelper.isEmptyString(dateStr)) {
return null;
}
SimpleDateFormat df = new SimpleDateFormat(dateFormat);
try {
return df.parse(dateStr);
} catch (Exception ex) {
return null;
}
}
示例13: onCreate
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_customized);
final Calendar nextYear = Calendar.getInstance();
nextYear.add(Calendar.YEAR, 1);
final Calendar lastYear = Calendar.getInstance();
lastYear.add(Calendar.YEAR, -1);
calendar = (CalendarPickerView) findViewById(R.id.calendar_view);
calendar.init(lastYear.getTime(), nextYear.getTime()) //
.inMode(SelectionMode.RANGE) //
.withSelectedDate(new Date());
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy");
String strdate = "16-06-2017";
String strdate2 = "14-06-2017";
try {
Date newdate = dateformat.parse(strdate);
Date newdate2 = dateformat.parse(strdate2);
ArrayList<Date> arrayList = new ArrayList<>();
arrayList.add(newdate);
arrayList.add(newdate2);
calendar.highlightDates(arrayList);
} catch (ParseException e) {
e.printStackTrace();
}
ArrayList<Integer> list = new ArrayList<>();
list.add(2);
list.add(3);
calendar.deactivateDates(list);
//initButtonListeners(nextYear, lastYear);
}
示例14: Response
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
public Response(JSONObject json) {
// status defaults to success
mStatus = new Status();
JSONObject error = json.optJSONObject(Keys.Error);
if (error != null) {
mStatus.setSuccess(false);
mStatus.setErrorMessage(error.optString(Keys.Message, "Unknown Error"));
mStatus.setStatusCode(error.optInt(Keys.Status, 500));
}
// top-level JSON properties
mConversationId = json.optString(Keys.ConversationId);
mParticipantId = json.optString(Keys.ParticipantId);
mETag = json.optString(Keys.ETag);
mAsrHypothesis = json.optString(Keys.AsrHypothesis);
mEndpoint = json.optString(Keys.EndpointHeader);
mTimedResponseInterval = json.optDouble(Keys.TimedResponseInterval, 0);
// last modified
String interval = json.optString(Keys.LastModified);
if (!interval.isEmpty()) {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
try {
mLastModified = dateFormat.parse(interval);
} catch (ParseException e) {
e.printStackTrace();
mLastModified = null;
}
}
// outputs
mOutputs = new ArrayList<>();
JSONArray outputs = json.optJSONArray(Keys.Outputs);
if (outputs != null) {
for (int i = 0; i < outputs.length(); i++) {
JSONObject output = outputs.optJSONObject(i);
if (output == null || !output.has(Keys.OutputType)) {
continue;
}
String type = output.optString(Keys.OutputType);
if (type.equals(OutputType.BEHAVIOR.toString())) {
mOutputs.add(new BehaviorOutput(output));
} else if (type.equals(OutputType.DIALOG.toString())) {
mOutputs.add(new DialogOutput(output));
}
}
}
// entities -- convert from JSON dictionary to an array
mEntities = new ArrayList<>();
JSONObject entities = json.optJSONObject(Keys.Entities);
if (entities != null) {
Iterator<String> keys = entities.keys();
while (keys.hasNext()) {
String key = keys.next();
Object entity = entities.opt(key);
if (entity == null) {
continue;
}
if (entity instanceof String) {
mEntities.add(new Label(key, (String)entity));
} else if (entity instanceof JSONArray) {
JSONArray arr = (JSONArray) entity;
ArrayList<Object> list = new ArrayList<>();
for (int i = 0; i < arr.length(); i++) {
Object item = arr.opt(i);
if (item != null) list.add(item);
}
mEntities.add(new List(key, list));
} else if (entity instanceof Boolean) {
mEntities.add(new Flag(key, (boolean)entity));
} else if (entity instanceof Number) {
mEntities.add(new Counter(key, ((Number)entity).doubleValue()));
}
}
}
}
示例15: testWeeksBetween
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
@Test
public void testWeeksBetween() throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd");
Date startDate = simpleDateFormat.parse("2017-06-01");
Date endDate = simpleDateFormat.parse("2017-06-18");
assertEquals(2, DaysBetweenUtil.weeksBetween(startDate, endDate));
}