How to convert the date format in java - java

String Resultmasterid=res1.getString(1);
System.out.println(Resultmasterid);
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMddHHMM");
Date varDate1=dateFormat1.parse(Resultmasterid);
dateFormat1=new SimpleDateFormat("dd-MMM-yyyy HH:MM");
String Final_admitDT=dateFormat1.format(varDate1);
This is my code I get the date as yyyyMMddHHMM format, now I need to change the format in dd-mmm-yyyy HH:MM. I get the result but it is not correct. Can any one help me on this please.

SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMddHHmm");
SimpleDateFormat("dd-MMM-yyyy HH:mm");
instead of
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMddHHMM");
SimpleDateFormat("dd-MMM-yyyy HH:MM");
SimpleDateFormat
MM indicates month
mm indicates minutes

You need to use
dateFormat1=new SimpleDateFormat("dd-MMM-yyyy HH:mm");
because mm represent the Minute in hour, whereas MM represent Month in year.
Have a look at the docs of SDF for more info on the patterns and pattern letters.

Your patterns are wrong, you should use MM for month and mm for minutes:
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMddHHmm");
...
dateFormat1 = new SimpleDateFormat("dd-MMM-yyyy HH:mm");
By the way, you should also respect the Sun code conventions and not use local variables starting with a capital letters (e.g. finalAdmitDT instead of Final_admitDT).

Related

How do I change 'AM' to 'PM' in a Date Object JAVA [duplicate]

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

Why won't my SimpleDateFormat convert correctly?

I have my Date as String: 2014-06-23 22:00
To get the Date as java.util.date I parse it by using SimpleDateFormat
Date listDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm",Locale.GERMANY);
listDate = sdf.parse(gameList.get(position).getTime());
but my output
System.out.println(listDate)
is 2014-04-118 19:37
What's going on here??
DD should be dd (in small) below are the available formats in Java7

Java Date format weird

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

Java Date, string convert to Date Object

I have a question, how can I convert a string like 20130706123020 to a date object.
So I what to convert the string 20130706123020 to a date object looking like:
2013-07-06 12:30:20
Attempted code:
String date = "20130706234310";
Date date1 = new SimpleDateFormat("yyyy-m-d H:m:s").parse(date);
System.out.println(date1);
Any suggestions will be appreciated.
Thank you!
You have to first parse the String using the parse method from SimpleDateFormat.
Then pass the Date object returned by the parse method to another SimpleDateFormat and then using the format method get the date in the format you want.
String s = "201307061230202";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS"); // format in which you get the String
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // format in which you want the date to be in
System.out.println(sdf1.format(sdf.parse(s)));
The significance of HH, hh, KK and kk in the hour field is different. I have used HH you can use the one according to your requirement.
H Hour in day (0-23)
k Hour in day (1-24)
K Hour in am/pm (0-11)
h Hour in am/pm (1-12)
Use SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
Date date = sdf.parse("20130706123020");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf2.format(date));
use this :
long int my_date = 20130706123020L
and after that :
String date_Text = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(my_date));

getting month name wrong with 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");

Categories

Resources