-3

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.

Eclipse code

Android Studio code

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Android isn't Java. The SDK's are different. So yes, you will get different results – Zoe May 21 '18 at 12:51
  • Can you please tell me how to solve this or get the right output on android studio? – Hasib Hasan May 21 '18 at 12:53
  • The code in your two screenshots is not identical. As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. May 21 '18 at 13:37
  • Please post your code as text in the question. Screen shots may be a nice supplement, but text is easier to read and also invites readers to copy and run it should we desire. Which will in the end help yourself. – Ole V.V. May 21 '18 at 13:47
  • Possible duplicate of [Conversion from 12 hours time to 24 hours time in java](https://stackoverflow.com/questions/6531632/conversion-from-12-hours-time-to-24-hours-time-in-java) – Ole V.V. May 21 '18 at 13:59
  • I had that problem figured out. But I'm now facing some other problem, I actually wanted to make an alarm app. Now time I'm trying to ring the alarm is on hh:mm format. But in the alarm manager it doesn't take hh:mm to check, rather it takes time on milliseconds. But when I convert that to on milliseconds its not as long as system time in milliseconds.as system counts year , month and date to while calculating to milli seconds. is there any way to make system count only hh:mm format while checking systems current time for alarm to ring? sorry for the long description. I'm new to this. – Hasib Hasan May 21 '18 at 19:05
  • alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeinmillisecond,AlarmManager.INTERVAL_DAY,pendingIntent); The thing that I am wanting to know is that here can I somehow use hh:mm format to set the alarm instead of calculating time in millisecond? – Hasib Hasan May 21 '18 at 19:20

4 Answers4

0

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();
    }
  • because you are passing In Upper latter in android and small in eclipes. –  May 21 '18 at 13:01
0

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;
}
Barns
  • 4,850
  • 3
  • 17
  • 31
  • is there any way to give time as hh:mm format in alarm manager on android studio instead of triggered after certain milliseconds? – Hasib Hasan May 21 '18 at 19:51
0

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;
    }
ankur uniyal
  • 86
  • 11
0

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. Uppercase HH for hour of day and lowercase mm for minute of hour. So you get the expected 14:30.
  • In your second screenshot you used HH:MM, that is, uppercase MM. 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 as 01 in your result, so you got 14:01. It’s very typical for SimpleDateFormat 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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161