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");
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 an issue where I would like to get the start of the day, however it seems to be setting it to 12:00 via automatic.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String todayDate = dfFull.parse("2014-06-06 00:00:00");
today = dfFull.format(todayDate);
System.out.println(today);
Why is this spitting out:
2014-06-06 12:00:00
The 12:00:00 is the issue
That is because hh represents the hour in 12 hour format. You need to use HH instead.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Have a look at the docs for more info.
Also, on a side note, there is a typo in your code.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date todayDate = dfFull.parse("2014-06-06 00:00:00"); // todayDate must be of type Date and not String
String today = dfFull.format(todayDate); // today should be of type String
System.out.println(today);
You should use HH for hours in this case.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String todayDate = dfFull.parse("2014-06-06 00:00:00");
today = dfFull.format(todayDate);
System.out.println(today);
Now you will get the out put as
2014-06-06 00:00:00
And again if you use hh that mean you are using 12 hour format while HH means 24 hour format
So
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");
I'm trying to parse a String describing a date (in french) :
String dateAParser="dim 6 janv 2013 07:40:00";
SimpleDateFormat parseur = new SimpleDateFormat("EEE dd MMMM yyyy HH:mm:ss", Locale.FRENCH);
try{
Date dateAllerDepart= new Date();
dateAllerDepart=parseur.parse(dateAParser);
System.out.println(dateAllerDepart);
}catch(Exception e){e.printStackTrace();}
It gives me these errors :
java.text.ParseException: Unparseable date: "dim 6 janv 2013 07:40:00"
at java.text.DateFormat.parse(DateFormat.java:357)
at TestAvecJsoup.main(TestAvecJsoup.java:109)
I think my SimpleDateFormat object is ok, and I searched and tried a lot of things to solve this problem, so I hope you will give some clues on how to solve it.Thank you in advance.
Two minor changes, adding periods after the abbreviations, and using 3 Ms instead of 4:
final String dateAParser = "dim. 6 janv. 2013 07:40:00";
final SimpleDateFormat parseur = new SimpleDateFormat("EEE dd MMM yyyy HH:mm:ss", Locale.FRENCH);
The first part of date String "dim" and month seems to be the issue here, are you sure it's correct value?
This works fine.
String dateAParser="06 2013 07:40:00 AM";
SimpleDateFormat parseur = new SimpleDateFormat("dd yyyy hh:mm:ss a", Locale.FRENCH);