convert String date to java.util.date in java [duplicate] - java

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 8 years ago.
how to convert string date to java.util.date in java .
String date="12-12-2014 10:00:00"
Date d=2014-12-12 10:00:00;
Need this string date in date formate

Try this:
String date = "12-12-2014 10:00:00";
DateFormat inputDateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
DateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(outputDateFormat.format(inputDateFormat.parse(date)));
Output:
2014-12-12 10:00:00
Basically you need to parse your date in the format provided and from one format (inputDateFormat) you need to convert it to the other format (done by outputDateFormat).

Try following code:
String test="12-12-2014 10:00:00";
Date date=new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").parse(test);
String OutputDate=new SimpleDateFormat("yyyy-dd-MM HH:mm:ss").format(date);
System.out.println(OutputDate);
Output :
2014-12-12 10:00:00
The dig over here is to parse a string with date attributes using SimpleDateFormat into java.util.Date.
For more on SimpleDateFormat visit this link.

Related

Is it possible to get Date object in Android in dd-mm-yyyy format? [duplicate]

This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
return date type with format in java [duplicate]
(1 answer)
Closed 4 years ago.
In Android all Date objects are given in this type of date format "Mon Dec 03 00:13:21 GMT+05:30 2018" when you convert it with SimpleDateFormat.
Is there any possibility of getting a Date object in the "dd-mm-yyyy" format in Android?
You could set the pattern on a SimpleDateFormat and format the date:
new SimpleDateFormat("dd-mm-yyyy").format(new Date());
You could convert Strings into Dates as the following:
String pattern = "dd-mm-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String strDate = simpleDateFormat.format(new Date());//firstly get formatted str
Date newDate = simpleDateFormat.parse(strDate);//then get date object with wanted format

Unparseable date: "2018-07-03T01:00:21.000+0000" Cannot parse this format [duplicate]

This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Java: Date parsing, why do I get an error
(3 answers)
Closed 4 years ago.
Try 1:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+/-HHmm");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);
Try 2:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);
Try 3:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);
Nothing seems to work with this format:
Any help?
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);
This might not exactly be what you want, but if the timezone offset would be written with a colon separator e.g. +00:00 it's the ISO_OFFSET_DATE_TIME
OffsetDateTime d = OffsetDateTime.parse("2018-07-03T01:00:21.000+00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
System.out.println(d); // 2018-07-03T01:00:21Z

Java Convert given date to date format like MM/dd/yyyy [duplicate]

This question already has answers here:
converting date format
(5 answers)
Closed 6 years ago.
I have a situation where i can get only the date like 9/2/2016. From this, i need to parse and create a pattern like M/D/YYYY. From this i can convert the any input date to the above pattern M/D/YYYY.
Is there any utility/API for that in java.?
For your requirement, first you parse the input string with appropriate format. Then you do format the date. For more info refer SimpleDateFormat
SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/yyyy");
Date myDate = dateFormat.parse("9/2/2016");
SimpleDateFormat dateFormat2 = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(dateFormat2.format(myDate));
Output
09/02/2016
You can use SimpleDateFormat to do this.
SimpleDateFormat dateFormat = new SimpleDateFormat(MM/dd/yyyy);
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Java - Converting yyyy-MM-dd'T'HH:mm:ssZ to readable dd-MM-yyyy [duplicate]

This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 6 years ago.
I will have a String input in the style of:
yyyy-MM-dd'T'HH:mm:ssZ
Is it possible to convert this String into a date, and after, parsing it into a readable dd-MM-yyyy Date Object?
Yes. It can be done in two parts as follows:
Parse your String to Date object
SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date dt = sd1.parse(myString);
Format the Date object to desirable format
SimpleDateFormat sd2 = new SimpleDateFormat("yyyy-MM-dd");
String newDate = sd2.format(dt);
System.out.println(newDate);
You will have to use two different SimpleDateFormat since the two date formats are different.
Input:
2015-01-12T10:02:00+0530
Output:
2015-01-12
//Parse the string into a date variable
Date parsedDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(dateString);
//Now reformat it using desired display pattern:
String displayDate = new SimpleDateFormat("dd-MM-yyyy").format(parsedDate);

How to get Date in the same format as String [duplicate]

This question already has an answer here:
Converting the format of the date in java
(1 answer)
Closed 1 year ago.
I want to get the Date object which contains Date in the form yyyy-mm-dd after adding few months to existing Date object.
Using DateFormat object I have tried this way but its not giving the output as I want. How can I get this Date format in Java?
My code-
Calendar c=Calendar.getInstance();
DateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");
Date d=cal.getTime();
c.add(Calendar.MONTH,10);
Date newd1=c.getTime();
String news=sdf.format(newd1);
Date dnew=(Date)sdf.parse(news);
String news has the date of format yyyy-mm-dd but when I use parse on that String it results Date object which is of format "Thu Jan 21 00:14:00 IST 2016". How can I get the Date object in the form of "yyyy-mm-dd" using String news in the above code.
java.util.Date doesn't have format. Its just Date.
You can make it print its values in any form you want, by using SimpleDateFormat
You can try as this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String news = sdf.format(new Date());
//for testing
System.out.println(news);

Categories

Resources