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

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);

Related

Reformat custom date from string [duplicate]

This question already has answers here:
Converting String to Date using SimpleDateFormat is returning random date [duplicate]
(2 answers)
Convert String Date to String date different format [duplicate]
(8 answers)
Closed 2 years ago.
I‘m fetching dates which have the format yyyy-MM-dd.
I have these extracted as a string which would look like this:
String date = "2020-09-05";
Now I want to convert it into a EEE, d MMM yyyy format, so the result should be: Sat, 5 Sep 2020.
I tried reformatting it like this:
Date convertedDate = new SimpleDateFormat(EEE, d MMM yyyy).parse(date);
For some reason this and many tries to get around it all end up with a java.text.ParseException: Unparseable date "2020-09-05".
Can‘t I convert a date from a string like that? Is there a better way to reformat a string into other formats?
You need to make a distinction between parsing the date (turning a String into a Date or LocalDate class) and formatting a date (turning the Date or LocalDate class instance to a String).
To parse the initial string, use:
LocalDate convertedDate = LocalDate.parse(date)
To format the convertedDate, use:
String formattedDate = convertedDate.format(DateTimeFormatter.ofPattern("EEE, d MMM yyyy"))
EDIT: I am assuming you are at least on Java 8, so you can use the newer date/time classes. See https://www.baeldung.com/java-8-date-time-intro for some more info.
You are mixing up parsing and formatting: your code is trying to parse a string given the format. You’re telling Java you’re expecting the date to contain the week name.
You need to first parse your date in the format it’s in. This will give you a date. You next need format your date with the desired format, which will give you a string:
final String dateString = "2020-09-05";
final Date date = new SimpleDateFormat("y-M-d").parse(dateString);
final String converted = new SimpleDateFormat("E, d MMM y").format(date);
ok here's the code:
String sDate1="1998-12-31";
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(sDate1);
System.out.println("EEE, d MMM yyyy formatted date : " + new SimpleDateFormat("EEE, d MMM yyyy").format(date));

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

Get current date in YYYY-MM-DD format in Java (Android) [duplicate]

This question already has answers here:
Month issue in SimpleDateFormat class
(6 answers)
Closed 5 years ago.
I am trying to get a Date object in the format YYYY-MM-DD representing the current system date. Below is my attempt:
Date todayDate = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
String todayString = formatter.format(todayDate);
The values are as follows:
todayDate: Mon May 15 16:24:47 GMT+01:00 2017
todayString: 2017-24-15
Having tried this a couple of times I noticed the todayString is not made up of YYYY-MM-DD but YYYY-[minutes][minutes]-DD.
How might I get the current date in the YYYY-MM-DD format?
Change this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
to this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
mm stands for the minutes, while MM (capitalized) stands for the month

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

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.

Get Date Object with formatted date [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 9 years ago.
I have a Date() object which always gives me date in a particular format (Comes in-built with Java). I want to get store Date in a specified format ("MMMM, dd yyyy HH:mm:ss"). I cam across one similar approach to do that-
Date date = new Date();
String dateFormat = "MMMM, dd yyyy HH:mm:ss";
SimpleDateFormat dt1 = new SimpleDateFormat(dateFormat);
dt1.format(date);
dt1.format(date) returns a formatted date in String data type. I want something similar which will return formatted date in Date data type.
I want something similar which will return formatted date in Date data type.
That is not possible. Date objects do not have a format by themselves, you cannot have a Date object in a specific format. You'll have to find another way to do what you need to do.
Your question is very unclear but I suspect what you want is parse a given String with a date into a Date object? If that is so, this should help: Java string to date conversion.
Given your example code, it would look like this:
String dateString = "June, 01 2014 08:23:51";
String dateFormat = "MMMM, dd yyyy HH:mm:ss";
Date date = new SimpleDateFormat(dateFormat, Locale.ENGLISH).parse(dateString);

Categories

Resources