我如何获取任何日期时间字符串的日期格式?

时间:2018-10-01 17:55:11

标签: java android simpledateformat

我从服务器收到此“ 10/1/2018,下午1:27:42”作为日期。现在要将其转换为SimpleDateFormat(“ yyyy-MM-dd HH:mm:ss”)。但是要做到这一点,我需要知道现有日期字符串的格式。 我如何找到正确的“ 10/1/2018,1:27:42 PM”格式?

1 个答案:

答案 0 :(得分:-1)

您需要使用SimpleDateFormat.toPattern()来获得图案。以这种方式使用此功能-

SimpleDateFormat sdf = new SimpleDateFormat();

// get the current date and time
Calendar cal = Calendar.getInstance();
String dateToday = sdf.format(cal.getTime());

// get the pattern used by the formatter and print it
String pattern = sdf.toPattern();
System.out.println("Pattern:"+pattern);