我需要解析以这种格式给出的日期,在Nexus 5上的Marshmallow上测试。
Nov 5, 2015, 03.44PM IST
我用Java SimpleDateFormat
解析它import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class Star {
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy, hh.mma z");
Date today= new Date();
Date date=null;
try {
date = format.parse("Nov 5, 2015, 03.44PM IST");
Calendar calendarToday =Calendar.getInstance();
calendarToday.setTime(today);
Calendar calendar =Calendar.getInstance();
calendar.setTime(date);
System.out.println(compareIgnoreTime(calendarToday,calendar));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int compareIgnoreTime(Calendar c1, Calendar c2) {
if (c1.get(Calendar.YEAR) != c2.get(Calendar.YEAR))
return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
if (c1.get(Calendar.MONTH) != c2.get(Calendar.MONTH))
return c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
return c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH);
}
}
虽然这种方法只投入Android
java.text.ParseException: Unparseable date: "Nov 5, 2015, 03.44PM IST" (at offset 22)
答案 0 :(得分:0)
首先获取日期并尝试格式化,在您获得有效日期之后,再次根据您的要求对其进行格式化。
String dtStart = new Date().toString();
SimpleDateFormat format = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
Locale.ENGLISH);
try {
Date date = format.parse(dtStart);
SimpleDateFormat print = new SimpleDateFormat("MMM dd, yyyy, hh.mma z");
System.out.println(print.format(date));
} catch (Exception e) {
e.printStackTrace();
}