Java Date To Another Conversion
Java Date To Another Conversion
How do I convert the date from one format to another date object in another format without using any
deprecated classes?
Ask Question
35
10
I'd like to convert a date in date1 format to a date object in date2 format.
java
Phoenix
3,246 13 39 74
I have created a simple method to do this. refer to stackoverflow.com/a/40042733/4531507 – Rahul Sharma Oct 14 '16 at 12:34
103
Use SimpleDateFormat#format :
Also note that parse takes a String , not a Date object, which is already parsed.
share improve this answer
João Silva
69.6k 23 132 144
This is what I tried and it threw an exception – Phoenix Sep 19 '12 at 22:08
you'd need to wrap Date parsedDate = simpleDateFormat1.parse(formattedDate); with try catch as it'd throw parseexception. – PermGenError Sep 19 '12 at 22:09
What are the exact inputs you are trying to parse? Try this demo ideone.com/Hr6B0. Perhaps you are passing an invalid Date ? – João Silva Sep 19 '12 at 22:11
My input is a string of this format August 21, 2012 and I need to save it as another string of format 20120821 – Phoenix Sep 19 '12 at 22:12
@Phoenix: Are you setting the Locale to Locale.ENGLISH of the first SimpleDateFormat ? Otherwise, if you are on a non-english Locale , August will not be parsed
correctly and will throw a ParseException. – João Silva Sep 19 '12 at 22:12
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
tl;dr
LocalDate.parse(
"January 08, 2017" ,
DateTimeFormatter.ofPattern( "MMMM dd, uuuu" , Locale.US )
).format( DateTimeFormatter.BASIC_ISO_DATE )
Using java.time
The Question and other Answers use troublesome old date-time classes, now legacy, supplanted by the java.time classes.
You have date-only values, so use a date-only class. The LocalDate class represents a date-only value without time-of-day and without time zone.
Your desired output format is defined by the ISO 8601 standard. For a date-only value, the “expanded” format is YYYY-MM-DD such as 2017-01-08 and the
“basic” format that minimizes the use of delimiters is YYYYMMDD such as 20170108 .
I strongly suggest using the expanded format for readability. But if you insist on the basic format, that formatter is predefined as a constant on the
DateTimeFormatter class named BASIC_ISO_DATE .
ld.toString(): 2017-01-08
output: 20170108
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date ,
Calendar , & SimpleDateFormat .
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find
some useful classes here such as Interval , YearWeek , YearQuarter , and more.
share improve this answer
Community ♦
1 1
Basil Bourque
111k 27 381 542
return newDate;
}
Please refer to the following method. It takes your date String as argument1, you need to specify the existing format of the date as argument2, and the result
(expected) format as argument 3.
try {
sdf = new SimpleDateFormat(givenformat);
sdf1 = new SimpleDateFormat(resultformat);
result = sdf1.format(sdf.parse(date));
}
catch(Exception e) {
e.printStackTrace();
return "";
}
finally {
sdf=null;
sdf1=null;
}
return result;
}
Peter Mortensen
13.7k 19 86 111
ThmHarsh
450 6 5
Dinesh Lomte
111 3
Try this
This is the simplest way of changing one date format to another
public String changeDateFormatFromAnother(String date){
@SuppressLint("SimpleDateFormat") DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
@SuppressLint("SimpleDateFormat") DateFormat outputFormat = new SimpleDateFormat("dd MMMM yyyy");
String resultDate = "";
try {
resultDate=outputFormat.format(inputFormat.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
return resultDate;
}
Sunil
1,712 1 13 29
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
try
{
//DateFormatdf = DateFormat.getDateInstance(DateFormat.SHORT);
DateFormat FromDF = new SimpleDateFormat(fromDateFormat);
FromDF.setLenient(false); // this is important!
Date FromDate = FromDF.parse(fromdate);
dateStringFrom = new
SimpleDateFormat(CheckFormat).format(FromDate);
DateFormat FromDF1 = new SimpleDateFormat(CheckFormat);
DF=FromDF1.parse(dateStringFrom);
System.out.println(dateStringFrom);
}
catch(Exception ex)
{
System.out.println("Date error");
output:- 15/03/2018
15 Mar 2018
jalpa jogi
1
getFormattedDate(
SimpleDateFormat(FormatUtils.d_MM_yyyy, Locale.getDefault()),
SimpleDateFormat(FormatUtils.d_MMM_yyyy, Locale.getDefault()),
dateOfTreatment
)
// 25 Nov 2017
val d_MMM_yyyy = "d MMM yyyy"
// 25/10/2017
val d_MM_yyyy = "d/MM/yyyy"
While SimpleDateFormat is not officially deprecated yet, it is certainly both long outdated and notoriously troublesome. Today we have so much better in java.time , the
modern Java date and time API and its DateTimeFormatter . Yes, you can use it on Android. For older Android see How to use ThreeTenABP in Android Project . – Ole V.V. Aug
29 '18 at 14:18
I didn't knew about that. I will check it soon – Killer Aug 29 '18 at 15:58
-1
Karthik M
56 5
SimpleDateFormat is limited to parsing millisecond precision, and will corrupt the minutes/seconds if asked to go beyond that. – Robert Dean May 13 '16 at 12:24
Your Answer
Sign up or log in
Sign up using Google
Post as a guest
Name
Email
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Not the answer you're looking for? Browse other questions tagged java or ask your own question.
Linked
-1
convert date from one format to another in java
-2
How can I Convert timestamp String to Date in
Android
-4
How to convert dd/MM/yyyy format date to
ccyy/MM/dd format in java?
0
Subtring to get date in android
-2
How to validate dates in two different formats
-3
Convert date format from dd-mm-yyyy to YYMMDD
126
How to use ThreeTenABP in Android Project
12
How to convert Date to a particular format in
android?
3
Simpledateformat unparseable date
2
How to format a date String into desirable Date
format
Related
1990
702
How do I convert from int to String?
224
32
Java : Cannot format given Object as a Date
1
convert string to date in java in format 2012-07-27
1
How to convert String to date object[without time]
275
-3
-3
Convert Date to the same format as another Date
question feed
STACK OVERFLOW
Questions
Jobs
Help
Mobile
Disable Responsiveness
PRODUCTS
Teams
Talent
Engagement
Enterprise
COMPANY
About
Press
Work Here
Legal
Privacy Policy
Contact Us
STACK EXCHANGE
NETWORK
Technology
Life / Arts
Culture / Recreation
Science
Other
Blog
Facebook
LinkedIn
site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required. rev 2019.2.15.32899