1

I am trying to use Date objects and calculate time differences for an android app. But I face a problem when time is in '12:00'. I mean when I input date as 12:12:00 Java AM/PM formatter returns 12:12:00AM but it should be 12:12:00PM.

I can't find any way to solve it.

Date date = new Date(); 
String stringDate = "2019-09-13 12:12:00";  
SimpleDateFormat formatter6=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
Date date6 = formatter6.parse(stringDate);   
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
System.out.println(sdf.format(date6));

It returns 12:12:00 AM but it should be 12:12:00 PM for correct calculations

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Saqeeb
  • 113
  • 6
  • 3
    Does changing `hh` to `HH` make any difference? – jsheeran Sep 13 '19 at 08:45
  • 2
    Use `SimpleDateFormat formatter6=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ` . – Viswanath Lekshmanan Sep 13 '19 at 08:46
  • 2
    Use `java.time`, especially the classes `LocalDateTime`, `ZonedDateTime` and `OffsetDateTime` along with `DateTimeFormatter`. That's the modern way… – deHaar Sep 13 '19 at 08:47
  • 2
    Do _not_ use SimpleDateFormat or recommend it – Joakim Danielson Sep 13 '19 at 08:47
  • So should `pm` be the standard for all times that don't define whether it's am or pm? If not, how should the parser figure out which one it should be? – Thomas Sep 13 '19 at 08:49
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Sep 14 '19 at 08:07

6 Answers6

3

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");
kumarchandresh
  • 559
  • 2
  • 16
2

Use DateTimeFormatter and LocalDateTime

String stringDate = "2019-09-13 12:12:00";  
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime date = LocalDateTime.parse(stringDate, formatter);
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("hh:mm:ss a");
System.out.println(formatter2.format(date));

You might also want to set a Locale for your second formatter depending on where you live.

DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("hh:mm:ss a", Locale.US);
System.out.println(formatter2.format(date));

12:12:00 PM

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
1

Pass the AM/PM in the time

Date date = new Date();
String stringDate = "2019-09-13 12:12:00 PM";
SimpleDateFormat formatter6 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
Date date6 = formatter6.parse(stringDate);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
System.out.println(sdf.format(date6));
CaffeinatedCod3r
  • 821
  • 7
  • 14
1

Try to do it the modern way, that is using java.time:

String stringDate = "2019-09-13 12:12:00";  
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");  
LocalDateTime datetime = LocalDateTime.parse(stringDate, dtf); 
DateTimeFormatter dtfA = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a");
System.out.println(datetime.format(dtfA));
// receive the time part and format it
LocalTime timePart = datetime.toLocalTime();
DateTimeFormatter tf = DateTimeFormatter.ofPattern("hh:mm:ss a");
System.out.println(timePart.format(tf));

This outputs

2019-09-13 12:12:00 PM
12:12:00 PM

on my system.

Note that your pattern String used for parsing is wrong since you are not using capital "H" for the hours of day, but "h" instead. That will definitely not work (correctly).

deHaar
  • 17,687
  • 10
  • 38
  • 51
0

Two solutions,

1.

Date date = new Date(); 
String stringDate = "2019-09-13 12:12:00 PM";  
SimpleDateFormat formatter6=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");

2.

Date date = new Date(); 
String stringDate = "2019-09-13 12:12:00";  
SimpleDateFormat formatter6=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Adisesha
  • 5,200
  • 1
  • 32
  • 43
0

If you are using java 8 or above then you should definitely use LocalDateTime and DateTimeFormatter makes it way easier to work with date times.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a");
String am = LocalDateTime.now().format(formatter);
String pm = LocalDateTime.now().plusHours(2).format(formatter);
System.out.println(am);
System.out.println(pm);

Now I am assuming that I run this code during am hours just 2 hours before it changes to pm you can also try out @Joakim Danielson answer which should not be dependent on when it is run.

checkout the documentation for LocalDateTime and DateTimeFormatter

Yantes
  • 251
  • 3
  • 18