本文整理汇总了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;
}