本文整理匯總了Java中java.text.DateFormat.parse方法的典型用法代碼示例。如果您正苦於以下問題:Java DateFormat.parse方法的具體用法?Java DateFormat.parse怎麽用?Java DateFormat.parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.text.DateFormat
的用法示例。
在下文中一共展示了DateFormat.parse方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: convert2StandardFormat
import java.text.DateFormat; //導入方法依賴的package包/類
/**
* 轉換日期格式
* @param 時間字符串 將yyyy-MM-dd HH:mm:ss 轉成 yyyyMMddHHmmss
*/
public static String convert2StandardFormat(String dateTimeStr) {
DateFormat dbDateFormat = dbDateFormatThreadLocal.get();
DateFormat dateFormat = dateFormatThreadLocal.get();
Date date;
String formatedDateStr = null;
try {
date = dbDateFormat.parse(dateTimeStr);
formatedDateStr = dateFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
};
return formatedDateStr;
}
示例2: compare
import java.text.DateFormat; //導入方法依賴的package包/類
/**
* 比較兩個日期的大小。<br>
* 若date1 > date2 則返回 1<br>
* 若date1 = date2 則返回 0<br>
* 若date1 < date2 則返回-1
* @autor:chenssy
* @date:2014年9月9日
*
* @param date1
* @param date2
* @param format 待轉換的格式
* @return 比較結果
*/
public static int compare(String date1, String date2,String format) {
DateFormat df = DateFormatUtils.getFormat(format);
try {
Date dt1 = df.parse(date1);
Date dt2 = df.parse(date2);
if (dt1.getTime() > dt2.getTime()) {
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
return -1;
} else {
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
示例3: compareDate
import java.text.DateFormat; //導入方法依賴的package包/類
/**
* 日期大小比較 date1大於date2返回1; date1小於date2返回-1 date1等於date2返回0
*
* @param date1
* @param date2
* @return
*/
@SuppressLint("SimpleDateFormat")
public static int compareDate(String date1, String date2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date dt1 = df.parse(date1);
Date dt2 = df.parse(date2);
if (dt1.getTime() > dt2.getTime()) {
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
return -1;
} else {
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
示例4: compare_date
import java.text.DateFormat; //導入方法依賴的package包/類
/**
* 兩個日期比較
* @param DATE1
* @param DATE2
* @return
*/
public static int compare_date(String DATE1, String DATE2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
try {
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(DATE2);
if (dt1.getTime() - dt2.getTime()>0) {//date1>date2
return 1;
} else {
return -1;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
示例5: test_setAndGetTimestamp
import java.text.DateFormat; //導入方法依賴的package包/類
@Test
public void test_setAndGetTimestamp() throws ParseException {
User user = new User();
Group group = new Group("Group 1");
Message message = new Message("Body", user, group);
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = dateFormat.parse("01/01/2017");
long time = date.getTime();
Timestamp timestamp = new Timestamp(time);
message.setTimestamp(timestamp);
assertEquals("timestamp not equal", message.getTimestamp(),date);
}
示例6: date
import java.text.DateFormat; //導入方法依賴的package包/類
/**
* Reads a "date" argument from the request.
* TODO handle timezones properly
*/
public Date date(ImapRequestLineReader request) throws ProtocolException {
char next = request.nextWordChar();
String dateString;
if (next == '"') {
dateString = consumeQuoted(request);
} else {
dateString = atom(request);
}
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
try {
return dateFormat.parse(dateString);
} catch (ParseException e) {
throw new ProtocolException("Invalid date format.");
}
}
示例7: durationFromAtoB
import java.text.DateFormat; //導入方法依賴的package包/類
public static long durationFromAtoB(String A,String B)
{
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long diff=0;
try
{
Date aDate=df.parse(A);
Date bDate=df.parse(B);
diff = bDate.getTime() - aDate.getTime();//這樣得到的差值是毫秒級別
}
catch(ParseException e)
{
e.printStackTrace();
}
return diff;
}
示例8: daysDifference
import java.text.DateFormat; //導入方法依賴的package包/類
/**
* Counts the days interval between two dates. Time component is ignored.
* @param date1
* @param date2
* @return
*/
public int daysDifference(Date date1, Date date2) {
// FIXME non è né efficiente né precisa ma funziona
// Presa da http://stackoverflow.com/questions/13198609/java-calendar-getting-difference-between-two-dates-times-off-by-one
try {
// Non credo sia necessario passare per un formatter: basta azzerare i componenti di ora, minuti etc. usando Calendar
DateFormat formatter= new SimpleDateFormat("MM/dd/yyyy");
String truncatedDateString1 = formatter.format(date1);
Date truncatedDate1 = formatter.parse(truncatedDateString1);
String truncatedDateString2 = formatter.format(date2);
Date truncatedDate2 = formatter.parse(truncatedDateString2);
long timeDifference = truncatedDate2.getTime()- truncatedDate1.getTime();
return Math.abs((int)(timeDifference / (24*60*60*1000)));
} catch (ParseException e) {
// Should never be
log.error("Failed to compute time difference", e);
}
return 0;
}
示例9: formTimeToDay
import java.text.DateFormat; //導入方法依賴的package包/類
public static String formTimeToDay(Context context, String time) {
Calendar today = Calendar.getInstance();
Calendar other = Calendar.getInstance();
DateFormat df_date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat df_time = new SimpleDateFormat("HH:mm");
try {
Date other_date = df_date.parse(time);
String tm = " " + df_time.format(other_date);
today.setTime(new Date());
today.set(11, 0);
today.set(12, 0);
today.set(13, 0);
other.setTime(other_date);
switch ((int) Math.floor((double) (((float) (other.getTimeInMillis() - today.getTimeInMillis())) / 8.64E7f))) {
case -1:
return context.getString(R.string.letvutil_week_yesterday) + tm;
case 0:
return context.getString(R.string.letvutil_week_tody) + tm;
case 1:
return context.getString(R.string.letvutil_week_tomorrow) + tm;
default:
return new SimpleDateFormat("MM" + LetvUtils.getString(R.string.month) + "dd" + LetvUtils.getString(R.string.day) + " HH:mm").format(df_date.parse(time));
}
} catch (ParseException e) {
return time;
}
}
示例10: getDate
import java.text.DateFormat; //導入方法依賴的package包/類
public Date getDate() {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = new Date();
try
{
date = df.parse(createdAt);
}
catch (ParseException e) {
Log.e("LOG", "ERROR", e);
}
return date;
}
示例11: dateUpload
import java.text.DateFormat; //導入方法依賴的package包/類
public static String dateUpload(String date) {
if (date != null) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
DateFormat plamberFormat = new SimpleDateFormat("dd.MM.yy", Locale.US);
Date newDate = null;
try {
newDate = format.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return plamberFormat.format(newDate);
} else {
return null;
}
}
示例12: usingDateFormatToParseDates
import java.text.DateFormat; //導入方法依賴的package包/類
@Koan
public void usingDateFormatToParseDates() throws ParseException {
DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
Date date2 = dateFormat.parse("01-01-2000");
assertEquals(date2.toString(), __);
// What happened to the time? What do you need to change to keep the time as well?
}
示例13: convertStringToDate
import java.text.DateFormat; //導入方法依賴的package包/類
public Date convertStringToDate(String dateString) {
Date formatteddate = null;
DateFormat df = new SimpleDateFormat("YYYYMMDD HH:mm:ss.SSS");
try{
formatteddate = df.parse(dateString);
}
catch ( Exception ex ){
return null;
}
return formatteddate;
}
示例14: getDateFromDateString
import java.text.DateFormat; //導入方法依賴的package包/類
public static Date getDateFromDateString(String dateString, String format, String timeZone) throws ParseException {
DateFormat simpleDateFormat = new SimpleDateFormat(format);
if (null != timeZone) {
simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
}
return simpleDateFormat.parse(dateString);
}
示例15: convertStringToFullDate
import java.text.DateFormat; //導入方法依賴的package包/類
public static Date convertStringToFullDate(String strDate) {
DateFormat df = getDateTimeFormat(_VN_DATE_TIME_FORMAT);
Date date = null;
try {
if (Validator.isNotNull(strDate)) {
date = df.parse(strDate);
}
}
catch (ParseException pe) {
_log.error(pe);
}
return date;
}