user2142786
user2142786

Reputation: 1482

How to display time in HHhMM a format in java ?

I want to display time in HHhMM format. For example if time is 09:30 AM i want to display it as 9h45 AM . I tried below approach :-

import java.util.*;
import java.text.*;

public class PS6 {

   public static void main(String args[]) {
      Date dNow = new Date( );
      SimpleDateFormat ft = 
      new SimpleDateFormat ("E yyyy.MM.dd 'at' HHhMM a");

      System.out.println("Current Date: " + ft.format(dNow));
   }
}

But Th e output was

Current Date: Fri 2016.10.21 at 14210 PM

which is incorrect.

Can anyone please help me in this.

Upvotes: 0

Views: 1815

Answers (3)

Meno Hochschild
Meno Hochschild

Reputation: 44061

In contrast to other answers given so far, you should also know that M stands for the month, not for the minute, and H stands for the 24-hour-clock which is questionable in context of having am/pm-marker. It is also worth to explicitly specify a locale.

So please use this pattern instead:

Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("E yyyy.MM.dd 'at' hh'h'mm a", Locale.ENGLISH);
System.out.println(
    "Current Date: " + ft.format(dNow)); // Current Date: Fri 2016.10.21 at 12h32 PM

Upvotes: 1

smsnheck
smsnheck

Reputation: 1593

If you use this one SimpleDateFormat ft = new SimpleDateFormat("E yyyy.MM.dd 'at' H'h'MM a"); you will get Current Date: Fr 2016.10.21 at 8h10 AM. I think this is what you're looking for

Upvotes: 1

Lahiru Ashan
Lahiru Ashan

Reputation: 767

Try this,

Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("E yyyy.MM.dd 'at' HH'h'MM a");
System.out.println("Current Date: " + ft.format(dNow));

Upvotes: 4

Related Questions