我正在尝试解析我的字符串格式为19, 1 19, 2016 12:25 pm
而我的SimpleDateFormat为EEEE, MMMM dd, yyyy hh:mm a
。我将上面的String解析为Tuesday, January 19, 2016 12:25 pm
,但它在这里抛出了parceable异常。我的代码如下,请在这里帮助我。
stestRequest = new SimpleDateFormat("EEEE, MMMM dd, yyyy hh:mm a");
// stringDate = "19, 1 19, 2016 12:25 pm"
dateRequestSelected = stestRequest.parse(stringDate);
请提前帮助我,谢谢。
答案 0 :(得分:1)
试试这个
ParsePosition pos = new ParsePosition(0);
SimpleDateFormat simpledateformat = new SimpleDateFormat("MM dd, yyyy hh:mm a"); //your date string format is : 1 19, 2016 12:25 pm
Date stringDate = simpledateformat.parse(date, pos);
SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE, MMMM dd, yyyy hh:mm a"); //Tuesday, January 19, 2016 12:25 pm,
System.out.println(dayFormat.format(stringDate));
答案 1 :(得分:0)
因此,如果您的日期为字符串1 19, 2016 12:25 pm
您需要定义与您的字符串相对应的日期格式:
SimpleDateFormat sdf = new SimpleDateFormat("MM dd',' yyyy hh:mm aa");
然后你必须解析字符串:
Date date = sdf.parse(dateString);
下一步是为输出定义格式:
SimpleDateFormat stestRequest = new SimpleDateFormat("EEEE',' MMMM dd',' yyyy hh:mm a");
然后你必须格式化:
stestRequest.format(date);
有关详细信息,请参阅SimpleDateFormat
的文档答案 2 :(得分:0)
试试这个......
String stringDate = "01 19, 2016 12:25 pm";
SimpleDateFormat oldSdf = new SimpleDateFormat("MM dd, yyyy hh:mm a");
SimpleDateFormat newSdf = new SimpleDateFormat("EEEE, MMMM dd, yyyy hh:mm a");
String dateRequestSelected = null;
try {
dateRequestSelected = newSdf.format(oldSdf.parse(stringDate));
Log.e("Date", dateRequestSelected);
} catch (ParseException e) {
e.printStackTrace();
}
输出:2016年1月19日,星期二,下午12:25
<强>编辑:强>
String stringDate = "01 19, 2016 12:25 pm";
SimpleDateFormat oldSdf = new SimpleDateFormat("MM dd, yyyy hh:mm a");
SimpleDateFormat newSdf = new SimpleDateFormat("EEEE, MMMM dd, yyyy hh:mm a");
Date dateRequestSelected = null;
try {
dateRequestSelected = newSdf.parse(newSdf.format(oldSdf.parse(stringDate)));
Log.e("Date", dateRequestSelected+"");
} catch (ParseException e) {
e.printStackTrace();
}
输出: Tue Jan 19 12:25:00 GMT + 05:30 2016
答案 3 :(得分:0)
新的Date()。toString()方法将为您提供所需格式的日期。您可以将其写入文件,然后在阅读时,您可以使用新的日期(字符串)解析日期。例如:
package Testers;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
public class Datestufff {
public void recordCurrentDate(File file){
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
String date = new Date().toString();
writer.write(date);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getDate(File file){
String date = "Unknown!";
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
date = line;
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return date;
}
public void recordDate(File file, Date date){
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(date.toString());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}