问题
So, I'm using SimpleDateFormat to convert a String which is "HH:MM AA" to "HH:MM", The problem is my code works fine on eclipse but when I run this on Android Studio it shows wrong output. Here are my both codes.


回答1:
The problem is you are using "MM" as minutes but it should be "mm". "MM" is for months.
The part "HH" is fine when you want the 24 hour values back.
try soemthing like this:
public static String getTimeFormatted(String time){
String s = "";
try{
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa", Locale.US);
Date d = sdf.parse(time);
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm", Locale.US);
s = formatter.format(d);
}
catch(Exception ex){
s = ex.getMessage();
}
return s;
}
回答2:
try this
public String changeDateFormatFromAnother(String date){
@SuppressLint("SimpleDateFormat") DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
@SuppressLint("SimpleDateFormat") DateFormat outputFormat = new SimpleDateFormat("dd MMMM yyyy");
String resultDate = "";
try {
resultDate=outputFormat.format(inputFormat.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
return resultDate;
}
回答3:
Try this code..
String date="2:30 PM";
SimpleDateFormat sdf5 = new SimpleDateFormat("hh:mm aa");
SimpleDateFormat sdf6 = new SimpleDateFormat("HH:mm");
try {
String str=sdf6.format(sdf5.parse(date));
Log.d("DATE TO:",str);
} catch (ParseException e) {
e.printStackTrace();
}
回答4:
java.time
String newTimeString = LocalTime
.parse("2:30 PM", DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH))
.toString();
System.out.println(newTimeString);
This prints
14:30
Even on Android you should consider not fighting with the age-old and notoriously troublesome SimpleDateFormat
class. java.time
, the modern Java date and time API, is much nicer to work with. A LocalTime
is a time of day without date and without time zone, so seems to match your requirements very neatly.
Question: Can I use java.time on Android?
Yes, java.time
works nicely on older and newer Android devices. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
What went wrong in your code?
- In your first screenshot, you are formatting using the format pattern
string
HH:mm
. UppercaseHH
for hour of day and lowercasemm
for minute of hour. So you get the expected14:30
. - In your second screenshot you used
HH:MM
, that is, uppercaseMM
. This is for month. Since the string you parsed didn’t have a month in it, it defaulted to January, which in turn was rendered as01
in your result, so you got14:01
. It’s very typical forSimpleDateFormat
that it agrees to print a month that was never there, just pretends that everything is fine, doesn’t inform you of an error. This is just one of the many reasons I recommend you avoid that class.
Links
- Oracle tutorial: Date Time explaining how to use
java.time
. - Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
来源:https://stackoverflow.com/questions/50449169/converting-string-to-time-using-simpledateformat-issues