I have a problem with String conversion to Date Format. Please help me. Below is my code:
String strDate = "23/05/2012"; // Here the format of date is MM/dd/yyyy
Now i want to convert the above String to Date Format like "23 May, 2012".
I am using below code but i am getting value as "Wed May 23 00:00:00 BOT 2012"
String string = "23/05/2012";
Date date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(string);
System.out.println(date); // Wed May 23 00:00:00 BOT 2012
How can i get the value as "23 May, 2012". Please help me friends....
You must render the date again.
You have the string and you parse it correctly back to a Date object. Now, you have to render that Date object the way you want.
You can use SimpleDateFormat again, changing the pattern.
Your code should then look like
String string = "23/05/2012";
Date date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(string);
String newFormat = new SimpleDateFormat("dd MMMM, yyyy").format(date);
System.out.println(newFormat); // 23 May, 2012
Use the method format() from the class SimpleDateFormat with the correct pattern.
Simple use:
SimpleDateFormat df = new SimpleDateFormat("dd MMM, yyyy");
System.out.println(df.format(date));
import java.text.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws ParseException{
String strDate = "23/02/2012";
Date date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(strDate);
String date1 = new SimpleDateFormat("dd MMMM, yyyy").format(date);
System.out.println(date1);
}
}
Related
String sDate = "06.08.2020" // 06 day 08 month 2020 is year
This is the date i have in my txt file. I use them in JTable. To sort the table i convert them to date with this DateFormatter.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
And it does convert the string to date as this.
LocalDate date = LocalDate.parse(sDate,formatter);
//The date : Thu Aug 06 00:00:00 TRT 2020
Now i need to convert it like the first date 06.08.2020.
But i can't use date as input. Because i get it from JTable so i get it as String.
So i tryed this code.
String sDate1 = "Thu Aug 06 00:00:00 TRT 2020";// The date i get from JTable
LocalDate lastdate = LocalDate.parse(sDate1,formatter);
sDate1 = formatter.format(lastdate);
But i get an error as this Text 'Thu Aug 06 00:00:00 TRT 2020' could not be parsed at index 0.
So this cone not works fine : LocalDate lastdate = LocalDate.parse(sDate1,formatter);
I cant see where is the problem.
I cannot reproduce the behaviour you describe. The following code worked fine for me:
import java.util.*;
import java.text.*;
public class MyClass {
public static void main(String args[]) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String date = "06.08.2020";
Date date1 = sdf.parse(date);
String result = sdf.format(date1);
System.out.println("Date = " + result);
}
}
Output: Date = 06.08.2020
That being said, if at all possible you should switch to the new java.time.* API.
Where your code failed:
SimpleDateFormat sdf1=new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
String dateStr = "06.08.2020";
sdf1.parse(dateStr);
As you can see, the pattern of the SimpleDateFormat and that of the date string do not match and therefore, this code will throw ParseException.
How to make it work?
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String dateStr = "06.08.2020";
Date date = sdf.parse(dateStr);
You must have already got why it worked. It worked because the pattern of the SimpleDateFormat matches with that of the dateStr string.
Can I format the Date object (i.e. date) into the original string?
Yes, just use the same format which you used to parse the original string as shown below:
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String dateStr = "06.08.2020";
Date date = sdf.parse(dateStr);
// Display in the default format
System.out.println(date);
// Format into the string
dateStr = sdf.format(date);
System.out.println(dateStr);
A piece of advice:
I recommend you switch from the outdated and error-prone java.util date-time API and SimpleDateFormat to the modern java.time date-time API and the corresponding formatting API (package, java.time.format). Learn more about the modern date-time API from Trail: Date Time.
Using the modern date-time API:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
String dateStr = "06.08.2020";
LocalDate date = LocalDate.parse(dateStr, formatter);
// Display in the default format
System.out.println(date);
// Format into the string
dateStr = formatter.format(date);
System.out.println(dateStr);
I don't see any difference using the legacy API and the modern API:
That's true for this simple example but when you will need to do complex operations using date and time, you will find the modern date-time API smart and clean while the legacy API complex and error-prone.
Demo:
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// Given date-time string
String strDate = "Thu Aug 06 00:00:00 TRT 2020";
// Replace TRT with standard time-zone string
strDate = strDate.replace("TRT", "Europe/Istanbul");
// Define formatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzzz yyyy");
// Parse the date-time string into ZonedDateTime
ZonedDateTime zdt = ZonedDateTime.parse(strDate, formatter);
System.out.println(zdt);
// If you wish, convert ZonedDateTime into LocalDateTime
LocalDateTime ldt = zdt.toLocalDateTime();
System.out.println(ldt);
}
}
Output:
2020-08-06T00:00+03:00[Europe/Istanbul]
2020-08-06T00:00
I have a database file with mm/dd/yy values for events, and I want to display the date as something similar to "Day(word), day(number), month(word)".
01/07/19 into
Monday 4th Jan or Monday 4 Jan or something similar.
Try something like this:
LocalDate.of(2019, 3, 2).format(DateTimeFormatter.ofPattern("EEE dd MMM YYYY"))
You can use SimpleDateFormat to convert the string to date and then convert back to String like this :
DateFormat format1 = new SimpleDateFormat("MM-dd-yyyy");
Date date = format1.parse("01-01-2019");
DateFormat format2 = new SimpleDateFormat("MMMMM dd, yyyy");
String dateString = format2.format(date);
System.out.println(dateString); //<- prints January 01, 2019
How to use the SimpleDateFormat?
Java provides a class called a SimpleDateFormat that allows you to format and parse dates in the as per your requirements.
You can use the above characters to specify the format - For example:
1) Date format required: 2019.01.01 20:20:45 PST
The appropriate date format specified will be- yyyy.MM.dd HH:mm:ss zzz
2) Date format required:09:30:00 AM 01-Jan-2019
The appropriate date format specified will be-hh:mm:ss a dd-MMM-yyyy
Tip: Be careful with the letter capitalization. If you mistake M with m, you will undesired results!
Let's learn this with a code example.
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDates_Format {
public static void main(String args[]) {
Date objDate = new Date(); // Current System Date and time is assigned to objDate
System.out.println(objDate);
String strDateFormat = "hh:mm:ss a dd-MMM-yyyy"; //Date format is Specified
SimpleDateFormat objSDF = new SimpleDateFormat(strDateFormat); //Date format string is passed as an argument to the Date format object
System.out.println(objSDF.format(objDate)); //Date formatting is applied to the current date
}
}
Output :
Sat Mar 02 16:37:59 UTC 2019
04:37:59 PM 02-Mar-2019
Have a nice day !
This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
Closed 6 years ago.
I have date in string object. I want to convert into Date object.
Date getDateFmString(String dateString)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdf.parse(dateString);
return convertedCurrentDate ;
}
above function returning following output.
Fri Apr 22 00:00:00 IST 2016
but I want output in this format '2016-03-01' only
function should take string only.
function should return Date object.
I have done lot of research over web, but I got solution from one Expert.
Date getDateFrmString(String dDate)
{
java.sql.Date dDate = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(sDate).getTime());
return dDate;
}
this is what I want.
Change the date format from
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
to
SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy");
Hope this works
See this example
public Class DateFormatDemo{
public static void main (String args[]) {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy);
String dateInString = "01/01/2015";
try{
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
}catch(ParseException e){
e.printStackTrace();
}
}
}
This link might help you with string to date object conversions
You are parsing with the wrong format try
String dateString="01-01-2016";
SimpleDateFormat sdfP = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdfP .parse(dateString);
String date=sdf.format(convertedCurrentDate );
System.out.println(date);
Output:
2016-01-01
DEMO1
And if you want the format to dd-MM-yyyy then no need to define seperate SimpleDateFormat object.
String dateString="01-01-2016";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date convertedCurrentDate = sdf.parse(dateString);
String date=sdf.format(convertedCurrentDate );
System.out.println(date);
OUTPUT:
01-01-2016
DEMO2
To format the string date you have to first parse the String to Date object using the same format of date which the String have then format it using the desired format as seen in the above code.
Date objects don't have a format. Only a String does. A Date object will be output with whatever format you tell it to be format as. It all depends on what the format of the DateFormat object is when you call .format(). Calling the toString() method on a Date object uses a DateFormat of "dow mon dd hh:mm:ss zzz yyyy".
Let's do it step by step:
You have a date as String in dd-MM-yyyy format.
You want to convert it into date. (for this you are using SimpleDateFormat)
Now you are printing the date. Question here is are you printing the converted date object or input string?
If its a date object then toString method is called of date class.
As per comment on java.util.Date class it's:
dow mon dd hh:mm:ss zzz yyyy
similar to
Fri Apr 22 00:00:00 IST 2016
So that coincides with what you get in output in the second approach. But how is that code even running is strange.
String inputStr = "11-11-2012";
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date inputDate = dateFormat.parse(input);
Variable 'input' is not defined.
What are the possible solutions:
While printing date, convert it back to String using SimpleDateFormat as per the requirement.
Date d =new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dStr = sdf.format(dateString);
System.out.printn(dStr);
Extending class java.util.Date and override toString, but that would be a bad idea.
My JSON object looks like
{"iso":"2014-01-01T21:13:00.000Z","__type":"Date"}
I need to get the date from it as a date Object.
What I have tried-
String dateStr = JsonObject.getString("iso"); //output is 2014-01-01T21:13:00.000Z
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date birthDate = sdf.parse(dateStr);
but this doesnt work, firstly it prompts me to add a try/catch which I do. When the debugger comes to Date birthDate = sdf.parse(dateStr); it just skips this.
How do I get a date out of my JSON object?
Use a date format like this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
to match the input string format.
For details, consult the documentation.
For example this
public static void main(String[] args) throws ParseException {
String dateStr = "2014-01-01T21:13:00.000Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Date birthDate = sdf.parse(dateStr);
System.out.println(birthDate);
}
prints (actual output depends on your time zone)
Wed Jan 01 22:13:00 CET 2014
I have to compare 2 dates which are in String format as: Fri Aug 23 17:03:19 IST 2013
I am trying to use new SimpleDateFormat("dd-MM-yyyy HH-mm-ss") to convert in DateTime so that dates can be comparable .
tempTimeStamp=Fri Aug 23 17:03:19 IST 2013
DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");
Date startDate;
startDate = df.parse(tempTimestamp);
String newDateString = df.format(startDate);
But its showing error object of this type can't be converted in DateFormat.Please help..
You were using the wrong format to try and parse the datetime string, try using the following snippet:
String tempTimeStamp="Fri Aug 23 17:03:19 IST 2013"
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date startDate = startDate = df.parse(tempTimestamp);
String newDateString = df.format(startDate);
You can look up more on the patterns for defining the date time format at http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Your expected format doesn't matches with the one provided.
The format of your SimpleDateFormat does not match that of the String that you are using.
The input String should match dd-MM-yyyy HH-mm-ss
If you are wanting simply to format the output of a Date, then no parsing of a String is necessary.