String startDate = "2018-07-29T09:50:49+05:30";
String TAG = "Extra";
final String TIMESTAMP_FORMATE = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
DateFormat df = new SimpleDateFormat(TIMESTAMP_FORMATE);
try {
Date date = df.parse(startDate);
System.out.println(TAG + "Start: " + date.getTime());
System.out.println(TAG + "Start: " + date.getDate());
System.out.println(TAG + "Start: " + date.getHours() + ":" + date.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
出现错误java.text.ParseException: Unparseable date: "2018-07-29T09:50:49+05:30"
知道我在这里缺少什么吗?
答案 0 :(得分:1)
在这种情况下,新的API变得更加容易。您的模式是java.time.ZonedDateTime
的默认格式:
ZonedDateTime date = ZonedDateTime.parse("2018-07-29T09:50:49+05:30")
答案 1 :(得分:1)
您可以尝试这样的事情
String time="2018-07-29T09:50:49+05:30";
ZonedDateTime date = ZonedDateTime.parse(time);
System.out.println(date);
String TAG = "Extra";
System.out.println(TAG + "Start: " + date.getDayOfMonth());
System.out.println(TAG + "Start: " + date.toLocalDateTime());
System.out.println(TAG + "Start: " + date.getHour() + ":" + date.getMinute()) ;
答案 2 :(得分:0)
You can use this method to get date and time for your date:
Below are the different formats of dates, you can use your own and pass it to the method as params.
public String localFormat = "yyyy-MM-dd HH:mm";
public String alarmFormat = "yyyy-MM-dd-HH-mm";
public String defaultFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
public String calendarFormat = "yyyy-M-d";
public String calendarFormatCh = "yyyy-M-dd";
public String calendarFormatRc = "yyyy-MM-dd";
public String reminderFormat = "dd/MM/yyyy hh:mm a";
public String getFormattedDate(Context mcontext, String date, String currFormat, String RequireFormat) {
Utils.e(Tag + "750", currFormat + "date " + date);
SimpleDateFormat sdf = new SimpleDateFormat(currFormat);
SimpleDateFormat sdfReq = new SimpleDateFormat(RequireFormat);
long time = 0;
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
try {
time = sdf.parse(date).getTime();
return sdfReq.format(time).toString();
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
只需以当前格式和您期望的格式传递日期,它将相应地返回您。如果只需要时间,则可以使用此方法,您将需要根据需要实现它。
答案 3 :(得分:-1)
您用来解析的格式也有毫秒级
final String TIMESTAMP_FORMATE = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
您需要将其更改为
final String TIMESTAMP_FORMATE = "yyyy-MM-dd'T'HH:mm:ss.XXX";
我尝试了以下示例,并且有效:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test{
public static void main(String[] args) {
String startDate="2018-07-29T09:50:49+05:30";
String TAG = "Extra";
final String TIMESTAMP_FORMATE = "yyyy-MM-dd'T'HH:mm:ssXXX" ;
DateFormat df = new SimpleDateFormat(TIMESTAMP_FORMATE);
try {
Date date = df.parse(startDate);
System.out.println(TAG + "Start: " + date.getTime());
System.out.println(TAG + "Start: " + date.getDate());
System.out.println(TAG + "Start: " + date.getHours() + ":" + date.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
}
}
输出:
ExtraStart: 1532838049000
ExtraStart: 29
ExtraStart: 9:1532838049000