Convert Time Value to Format "Hh:Mm Am/Pm" Using Android

Convert time value to format “hh:mm Am/Pm” using Android

I got answer just doing like this.

startTime = "2013-02-27 21:06:30";
StringTokenizer tk = new StringTokenizer(startTime);
String date = tk.nextToken();
String time = tk.nextToken();

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a");
Date dt;
try {
dt = sdf.parse(time);
System.out.println("Time Display: " + sdfs.format(dt)); // <-- I got result here
} catch (ParseException e) {
e.printStackTrace();
}

How to convert HH:mm:ss to hh:mm AM/PM in android

Use SimpleDateFormat to convert from One Time/Date Format to Another.

Log.v("12HRS Time", getFormatedDateTime("23:30:00", "HH:mm:ss", "hh:mm a"))

public static String getFormatedDateTime(String dateStr, String strReadFormat, String strWriteFormat) {

String formattedDate = dateStr;

DateFormat readFormat = new SimpleDateFormat(strReadFormat, Locale.getDefault());
DateFormat writeFormat = new SimpleDateFormat(strWriteFormat, Locale.getDefault());

Date date = null;

try {
date = readFormat.parse(dateStr);
} catch (ParseException e) {
}

if (date != null) {
formattedDate = writeFormat.format(date);
}

return formattedDate;
}

For SimpleDateFormat Reference:- https://developer.android.com/reference/java/text/SimpleDateFormat.html

How to convert hh:mm AM/PM string to formatted time?

This might not be a best solution. But what you can do is convert your input time to seconds and then compare integer values.

Below function can help you give an idea on getting seconds. (just a pseudo code. Not a tested one)

public int getSeconds(String timeValue){

String[] splitByColon = timeValue.split(":");
int hoursValue = Integer.parseInt(splitByColon[0]));

String[] splitForMins = splitByColon[1].split(" ");

if(splitForMins[1].equals("PM"))
{
hoursValue = hoursValue + 12;
}

int minutesValue = Integer.parseInt(splitForMins[0]);

return 3600*hoursValue + 60*minutesValue;

}

Display current time in 12 hour format with AM/PM

Easiest way to get it by using date pattern - h:mm a, where

  • h - Hour in am/pm (1-12)
  • m - Minute in hour
  • a - Am/pm marker

Code snippet :

DateFormat dateFormat = new SimpleDateFormat("hh:mm a");

Read more on documentation - SimpleDateFormat java 7

Android : How can I convert a Time with am and pm format into integer seconds

You can use a SimpleDateFormat to convert the String into a Date

String myStringDate = "10:40 PM"
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
Date myDate = sdf.parse(myStringDate);

Then get time in milliseconds and replace it in your query:

Calendar calendar = Calendar.getIstance();
calendar.setTime(myDate);
int hourToSeconds = calendar.get(Calendar.HOUR_OF_DAY) * 60 * 60;
int minutesToSeconds = calendar.get(Calendar.MINUTE) * 60;

int totalSeconds = hourToSeconds + minutesToSeconds ;
Long alertTimeCatcher = new GregorianCalendar().getTimeInMillis()+ totalSeconds *1000;

SimpleDateFormat AM/PM conversion

java.time and ThreeTen Backport

    DateTimeFormatter timeFormatter
= DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);

String time24Hour = "12:10:00";
LocalTime time = LocalTime.parse(time24Hour);
String time12Hour = time.format(timeFormatter);
System.out.println(time12Hour);

Output from this snippet is what you expected:

12:10 PM

Notice that your format pattern string, hh:mm a, is correct for formatting. I don’t know what you used for parsing, I suspect that your error may have been there.

I have added a locale to the formatter to control which language I get. Since AM and PM are hardly used in other languages than English, I chose Locale.ENGLISH, but please choose the locale that is right for you.

The SimpleDateFormat class that you used is notoriously troublesome and fortunately long outdated. Instead I am using java.time, the modern Java date and time API. This has the added bonus that parsing the 24 hour format goes smoothly without an explicit formatter. This is because the format you’ve got conforms with ISO 8601, the internation date and time standard. The modern classes parse (and also print) ISO 8601 format as their default.

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) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern 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.

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.
  • Wikipedia article: ISO 8601

How do I change 'AM' to 'PM' in a Date Object JAVA

In Line:

SimpleDateFormat formatter6=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

The hh makes sure that hours are parsed as AM/PM values b/w 1-12. To get the desired result, you can use HH marker which parses hour values between 0-23. So, the code should be:

SimpleDateFormat formatter6=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Getting wrong value for AM/PM from 12:00 to 12:59 when converting from 24-hour format to 12 hour format in Kotlin Android (Minimum Android Version 21)

Got it fixed. There was a minor error.

I was using hh as input format, which is 12-hour format. I needed to use HH instead.

return outputFormat.format(SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US).parse(this))

Android DateFormat for AM/PM differs between devices

You may either have to use either the 24 hour value to determine what to append so that you can add the format you desire.

public static final String TIME = "hh:mm";

and then

String ampm = Integer.parseInt(time.valueOf("hh")) >= 12 ? "PM" : "AM";
...
return mDateFormat.format(date)+" "+ampm;

Or if you feel lazy you can just do without changing the value of TIME:

return mDateFormat.format(date).toUpperCase().replace(".","");


Related Topics



Leave a reply



Submit