Can you please suggest how to display Timestamp value in format "dd MonthName" format for e.g. "Tue Jun 25 21:56:17 IST 2013" should be display as "25 June" . I'm using below code but it is not working .
Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat ("d M");
System.out.println("Current Date: " + ft.format(dNow));
You are using a wrong pattern. your pattern should be:
SimpleDateFormat ft = new SimpleDateFormat ("dd MMM");
Use the format as "dd MMMMM" as
dd: Day in month (2 digit day number)
MMMMM: Month in year
SimpleDateFormat sdf = new SimpleDateFormat ("dd MMMMM");
Read more about SimpleDateFormat here
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Pattern for "25 June" should be:
SimpleDateFormat ft = new SimpleDateFormat ("d MMMM");
Related
This question already has answers here:
Comparing two times in android
(4 answers)
12:xx shown as 00:xx in SimpleDateFormat.format("hh:mm:ss")
(1 answer)
Closed 3 years ago.
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
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");
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
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));
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).
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");
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
When I convert the String "07/02/2014" (mm/dd/yyy) to a java.util.Date using SimpleDateFormatter I get a result of Sun Dec 29 00:00:00 CAT 2013.
Here is the code I am running:
DateFormat formatter;
formatter = new SimpleDateFormat("MM/DD/YYYY");
Date exactDate = formatter.parse("07/02/2014");
Why is this happening ?
It must be:
DateFormat formatter;
formatter = new SimpleDateFormat("MM/dd/yyyy");
Date exactDate = formatter.parse("07/02/2014");
The documentation explains why.
y Year
Y Week year
D Day in year
d Day in month
M Month in year
m Minute in hour
Try this one,
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date exactDate = formatter.parse("07/02/2014");
When you print the Date object you will get that output. Try formatting Date into String for desired format and output.
Try following code:
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date exactDate = formatter.parse("07/02/2014");
String date=new SimpleDateFormat("MM/dd/yyyy").format(exactDate);
Hope this helps.
I have time on device 11:34
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy', 'hh:mm");
Date date_current = new Date();
Date date_start = null;
date_start = sdf.parse("12.03.2014, 12:00");// I PARSE THIS DATE!!!
RESULT IS :
date_start:
Wed Mar 12 00:00:00 Восточноевропейское время 2014
BUT SHOULD BE:
Wed Mar 12 12:00:00 Восточноевропейское время 2014
HOW to solve it?
To get 24h format use HH not hh. In 12h format hours can be in rage 0-11, which makes 12 overflow to 0.
Use
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy', 'HH:mm");
use 24 hour date format
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy', 'HH:mm");
First of all take a look about patterns of Simpledatrformat. where it clearly shows H is for (0-23).
Reference for Date Format Pattern Syntax
so you should change your code like below.
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy', 'HH:mm");
Date date_current = new Date();
Date date_start = null;
date_start = sdf.parse("12.03.2014, 12:00");
System.out.println("now time is.." + date_start);
OR
Use this:
Date date = new Date();
date.setHours(date.getHours() + 8);
System.out.println(date);
SimpleDateFormat simpDate;
simpDate = new SimpleDateFormat("kk:mm:ss");
System.out.println(simpDate.format(date));
Thanks.. use above code to parse correctly!!
String dateString="2001/03/09";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
Date convertedDate = dateFormat.parse(dateString);
System.out.println("Converted string to date : " + convertedDate);
i get output as follows:
Converted string to date : Tue Jan 09 00:03:00 IST 2001
What is wrong with my code?
Use MM instead of mm for months - mm means minutes, not months.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
See the API documentation of SimpleDateFormat.
I am having problem with this small piece of code
SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
String str = "2010-03-13 01:01:22";
Date date = sf.parse(str);
SimpleDateFormat f = new SimpleDateFormat("d MMM yyyy hh:mm aaa");
System.out.println(" Date " + f.format(date));
Output :
Date 13 Jan 2010 01:01 AM
The code seems to be fine but still I am getting month name wrong.
Please help !!
Thanks.
You are using minute instead of month in your pattern. It should be:
yyyy-MM-dd HH:mm:ss
mm stands for minute. You should use MM when parsing month:
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");