Convert date from m/d/yy to mm/dd/yyyy [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Conversion of Date
How to convert date from m/d/yy to mm/dd/yyyy?
I get the date in a String as: 5/1/12.
I need to convert it to 05/01/2012.
Please suggest.
I used the following code:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
d_date = dateFormat.parse("5/1/12");
strDate = dateFormat.format(d_date);
Result:
05/01/0012
Expected Result:
05/01/2012
Thanks
Prasad

Try the following:
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date = (Date)formatter.parse("5/1/12");
formatter = new SimpleDateFormat("MM/dd/yyyy");
date = (Date)formatter.format(date);
The following links should prove helpful for more complicated tasks:
Oracle SimpleDateFormat Docs
Example code

Related

Issue with converting String to Date [duplicate]

This question already has answers here:
Comparing time is incorrect when picking 12:00
(3 answers)
Closed 4 years ago.
I tried to convert a String to Date by the following way in Java:
public static Date dateConv1(String s){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");
String dateInString = s;
Date date = new Date();
It converts the noon 12:00-12:59 as midnight i.e 00:00-00:59. Could soeone let me know how I should solve this issue?
Change your formatter:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
HH - means hours
Reach to: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html and see all the examples you need.
The String is not correctly formated. hh means 1-12 hours. There are 4 way to format hours (hh, HH, kk, KK).
Here you can read about the formatting:
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");
" hh " is for 12 hours format change it to
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

SimpleDateFormat and Date object [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Month name as a string
(12 answers)
Closed 6 years ago.
In my android app I retrieve from an api the current day as.
long dateTime = innerJSON.getLong("dt");
Date weekDay = new Date(dateTime * 1000L);
SimpleDateFormat outFormat = new
SimpleDateFormat("EEEE");
String day = outFormat.format(weekDay);
And I get the current day ie Monday.
For the date I use the same Date object but with a different SimpleDateFormat object.
SimpleDateFormat outFormat1 = new SimpleDateFormat("dd-M-yyyy");
String date = outFormat1.format(weekDay);
And that gives me,
28-5-2016
which is fine. However, I want the date to have a format of
28 May
Any ideas on that? I checked the official orarcle's site,but I can't find a solution.
Thanks,
Theo.
Try this
SimpleDateFormat outFormat1 = new SimpleDateFormat("dd MMM");
you will get the output you required.
You need to use dd MMM format here.
More details provided here in official documentation.
SimpleDateFormat outFormat1 = new SimpleDateFormat("dd MMM");
String date = outFormat1.format(weekDay);
haha. That was easy.
SimpleDateFormat("dd MMM");
It's summer now and sunny,so I can't concetrate:):)

Java how to convert UK into java.util.Date [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 7 years ago.
I'm retrieving a date in a HTML form, and I need to convert the string date into a java.util.Date so that it can be inserted into a database.
I've tried the following, but it always gives me a date in May 2008.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = formatter.parse("27-11-2015");
String string = "2015-11-27";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);
This was user error, my code should have been:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date myDate = formatter.parse("27-11-2015");

Represent MM-DD-YYYY as Day, Month Date [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 7 years ago.
I want to represent date in the format MM-DD-YYYY as Day, Month Date
For example - represent 5-12-2015 as Tuesday, May 12. How can I do this?
Though I am giving you the code to do this, you should first show us your efforts.
String date = "5-12-2015";
DateFormat format = new SimpleDateFormat("MM-dd-yyyy");
Date d = format.parse(date);
DateFormat format1 = new SimpleDateFormat("EEEE, MMM dd");
String finalDateString = format1.format(d);
System.out.println(finalDateString);

Date Conversion from String to sql Date in Java giving different output? [duplicate]

This question already has answers here:
Java date format conversion - getting wrong month
(8 answers)
Closed 3 years ago.
I have a string form of Date. I have to change it to Sql Date. so for that i have used the following code.
String startDate="01-02-2013";
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-mm-yyyy");
java.util.Date date = sdf1.parse(startDate);
java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());
when i used the above code and run that. I got the following output.
2013-01-01.
Here Month is not converted correctly.
Please tell me where is the problem and provide sample code to get correct result?
mm is minutes. You want MM for months:
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
Don't feel bad - this exact mistake comes up a lot.
mm stands for "minutes". Use MM instead:
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
You need to use MM as mm stands for minutes.
There are two ways of producing month pattern.
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy"); //outputs month in numeric way, 2013-02-01
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy"); // Outputs months as follows, 2013-Feb-01
Full coding snippet:
String startDate="01-Feb-2013"; // Input String
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy"); // New Pattern
java.util.Date date = sdf1.parse(startDate); // Returns a Date format object with the pattern
java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());
System.out.println(sqlStartDate); // Outputs : 2013-02-01
That is the simple way of converting string into util date and sql date
String startDate="12-31-2014";
SimpleDateFormat sdf1 = new SimpleDateFormat("MM-dd-yyyy");
java.util.Date date = sdf1.parse(startDate);
java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());
While using the date formats, you may want to keep in mind to always use MM for months and mm for minutes. That should resolve your problem.

Categories