-1

How to get time in following format in android programatically? Format eg: " 03:45 p.m ". In default we are using simple dateformat " hh:mm a ". But its output looks like PM/AM. I want time exactly like p.m/a.m. Since in samsung galaxy J7 prime has this type default time and date. Please let me know the solution or anything wrong with my question. Thanks in advance...

Exactly i need to fetch timestamp to this following format "03:45 p.m". Already prepared function but doubt in time format. Instead of this "hh:mm a" which format has to use?

  • Are you telling that you want to display the AM/PM in lower case? – SripadRaj Jun 27 '17 at 10:34
  • i need to fetch timestamp to this following format "03:45 p.m". Already prepared function but doubt in time format. instead of this "hh:mm a" which format has use? –  Jun 27 '17 at 10:40
  • The linked question is asking for `am` and `pm` without the dot, but I think you can use the same answers and just fill in `a.m` instead of `am` and similarly for `p.m`. If you are having any issue with it, please revert. – Ole V.V. Jun 27 '17 at 10:45

2 Answers2

2

You can use this class which I use . You need to pass the Date to the constructor and

 public static CustomDate getYrMonthDateFormateString(String datestring) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",Locale.getDefault());
    Date d = sdf.parse(datestring);
    return new CustomDate(d);
 }

 public static class CustomDate {

    final String dayOfTheWeek;
    final String day;
    final String monthString;
    final String monthStringComplete;
    final String monthNumber;
    final String year;
    final String time;

    CustomDate(Date date) {

      dayOfTheWeek = (String) DateFormat.format("EEE", date); // Thursday
      day = (String) DateFormat.format("dd", date); // 20
      monthString = (String) DateFormat.format("MMM", date); // Jun
      monthStringComplete = (String) DateFormat.format("MMMM", date); // June
      monthNumber = (String) DateFormat.format("MM", date); // 06
      year = (String) DateFormat.format("yy", date); // 2013
      time = ((String) DateFormat.format("h:mm a", date)).toLowerCase().replace(" ", ""); // 6:00 am
    }

    public String getDayOfTheWeek() {
      return dayOfTheWeek;
    }

    public String getDay() {
      return day;
    }

    public String getMonthString() {
      return monthString;
    }

    public String getMonthNumber() {
      return monthNumber;
    }

    public String getYear() {
      return year;
    }

    public String getTime() {
      return time;
    }

    public String getMonthStringComplete() {
      return monthStringComplete;
    }

 }
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
MRX
  • 1,400
  • 3
  • 12
  • 32
1

You can convert text by yourself. Smth like this:

 private String getTime() {
    SimpleDateFormat sdf = new SimpleDateFormat("kk:mm a");
    String formated = sdf.format(new Date());
    String[] split = formated.split(" ");
    String amPm = split[1];
    String time;
    if(amPm.equals("PM")) {
        time = String.format("%s %s",split[0], "p.m.");
    } else {
        time = String.format("%s %s",split[0], "a.m.");
    }
    Log.e("MainActivity", "onClick: " + time);
    return time;
}
kara4k
  • 407
  • 5
  • 11