-3

i am stuck at this ,i have timestamp as string variable , and i want to convert it to this pattern DayName-Month-year at 04:00 pm ,the problem is i am targeting older version of android so when use SimpleDateFormat it shows error as required higher android API . please help

Rech K
  • 7
  • 7
  • [SimpleDateFormat was added in API level 1](https://developer.android.com/reference/java/text/SimpleDateFormat). – Andy Turner Jul 16 '18 at 19:14
  • 1
    *"i have timestamp as string variable"* In what format? --- *"i am targeting older version of android"* What API level? --- *"it shows error"* What error? Show us. --- *"DayName-Month-year at 04:00 pm"* So `Mon-Jan-2018 at 04:00 pm`? That doesn't look right. – Andreas Jul 16 '18 at 19:14
  • Have you solved it or haven't? – Shahzad Afridi Jul 16 '18 at 19:44
  • Frankly I don’t know the exact API level requirement, but even for low API levels you may add [the ThreeTenABP library](https://github.com/JakeWharton/ThreeTenABP) to your Andoird project and use `java.time`, the modern Java date and time API. Since the old-fashioned `SimpleDateFormat` is so notoriously troublesome, it may be worth considering. – Ole V.V. Jul 16 '18 at 20:18
  • Formatting date-time values on Android (and Java) have been covered many many times already. Always search Stack Overflow thoroughly before posting. – Basil Bourque Jul 16 '18 at 22:10
  • 1
    More duplicates: [this](https://stackoverflow.com/q/20311306/642706), [this](https://stackoverflow.com/q/14890388/642706), [this](https://stackoverflow.com/q/454315/642706), [this](https://stackoverflow.com/q/30340308/642706), [this](https://stackoverflow.com/q/32911677/642706). – Basil Bourque Jul 16 '18 at 22:16

1 Answers1

2

If you are getting API error, It's because you imported android.icu.text.SimpleDateFormat instead of java.text.SimpleDateFormat.

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

Date dt = new Date(date1);
SimpleDateFormat sdf = new SimpleDateFormat("dd:MM:yyyy hh:mm a");
String time1 = sdf.format(dt);

OR

Date date = new Date();
String strDateFormat = "dd:MM:yyyy hh:mm a";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
String time1 = sdf.format(date);
Shahzad Afridi
  • 2,058
  • 26
  • 24