String str2="April 30, 2013";
simpleDateFormat s= new simpleDateFormat();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
Date dateObject = null;
I have string like "April 30,2013", I am getting this as string from form. I want to convert this into date object as 04/30/2013. Is there any way to do it?
I tried with simpleDateFormat() but I didn't found proper output.
Use MMMMM dd, yyyy to parse and MM/dd/yyyy to format
Use the following code.
String str2="April 30, 2013";
SimpleDateFormat input = new SimpleDateFormat("MMMM dd, yyyy");
SimpleDateFormat output = new SimpleDateFormat("MM/dd/yyyy");
Date date = input.parse(str2);
System.out.println(output.format(date));
This is how you can convert it String date to Date object:
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM d,yyyy");
SimpleDateFormat yourNewDateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = dateFormat.parse("April 30,2013");
System.out.println(yourNewDateFormat.format(date));
Related
i have a common doubt for a long while. During date formation if the input date in the format like "2019/05/22 02:00:23" then with the help following line we can format the date,
String inputDate = "2019/05/22 02:00:23";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);
I can see both input format and the required format is same. Suppose if i change the input date like below, it is showing the java.text.ParseException: Unparseable date: exception.
String inputDate = "31.12.9999";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);
Please suggest how to achieve this.
Please try below i mentioned date format code,I hope it will help you,
From this,
String inputDate = "31.12.9999";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);
To Change:
Date date = new SimpleDateFormat("dd.MM.yyyy").parse("31.12.9999");
String formattedDate = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(date);
System.out.println("formattedDate :: " + formattedDate);
Thanks,.
I am trying to format the following input date: "2019-02-12 18:00:40""
to the following format "dd-MM-yyyy". However, I am experiencing mixed results with the date formatter method I created below and the output is as follows
"Wed Aug 11 00:00:00 GMT+02:00 17"
private String formatDate(String dateT) throws ParseException
{
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date date = formatter.parse(dateT);
return date.toString();
}
As mentioned, you'll need two formats to get your desired result.
If you can use Java8+, I suggest using LocalDateTime and DateTimeFormatter (instead of SimpleDateFormat):
String stamp = "2019-02-12 18:00:40";
LocalDateTime ldt = LocalDateTime.parse(stamp, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(ldt.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")));
Output:
12-02-2019
Edit:
If you really must use the outdated classes, you can apply the same principle with SimpleDateFormat:
String stamp = "2019-02-12 18:00:40";
SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dt1.parse(stamp);
SimpleDateFormat dt2 = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(dt2.format(date));
As suggested by #Robert. This was the solution I ended up using with two simpledateformatters.
private String formatDate(String date) throws ParseException {
SimpleDateFormat inputDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentDate = inputDate.parse(date);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate = formatter.format(currentDate);
return formattedDate;
}
I have my Date as String: 2014-06-23 22:00
To get the Date as java.util.date I parse it by using SimpleDateFormat
Date listDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm",Locale.GERMANY);
listDate = sdf.parse(gameList.get(position).getTime());
but my output
System.out.println(listDate)
is 2014-04-118 19:37
What's going on here??
DD should be dd (in small) below are the available formats in Java7
I want this format 6 Dec 2012 12:10
String time = "2012-12-08 13:39:57 +0000 ";
DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date date = sdf.parse(time);
System.out.println("Time: " + date);
You need to first parse your date string (Use DateFormat#parse() method) to get the Date object using a format that matches the format of date string.
And then format that Date object (Use DateFormat#format() method) using the required format in SimpleDateFormat to get string.
String time = "2012-12-08 13:39:57 +0000";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse(time);
String str = new SimpleDateFormat("dd MMM yyyy HH:mm:ss").format(date);
System.out.println(str);
Output: -
08 Dec 2012 19:09:57
Z in the first format is for RFC 822 TimeZone to match +0000 in your date string. See SimpleDateFormat for various other options to be used in your date format.
change SimpleDateFormat to:
String time = "2012-12-08 13:39:57 +0000";
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
Date date = sdf.parse(time);
DateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
String formatedTime = sdf.format(date);
System.out.println("Time: " + formatedTime);
Take a look at SimpleDateFormat. The code goes something like this:
SimpleDateFormat fromUser = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
String reformattedStr = myFormat.format(fromUser.parse(inputString));
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd-MM-yyyy");
Date date=simpleDateFormat.parse("23-09-2008");
You can use the SimpleDateFormat Class to do this!
such:
DateFormat mydate1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = mydate1.parse(time);
How would I convert a String date in the form of 24/04/2012 to a date variable in the format of 24-Apr-12, which can later be passed into my Oracle database.
I have tried this but it says the string date is unparsable:
DateFormat format = new SimpleDateFormat("dd-MMM-yy");
Date newDate = (Date) format.parse(date);
I think you are confusing parsing and formatting, try this:
String old_date = "24/04/2012";
DateFormat old_format = new SimpleDateFormat("dd/MM/yyyy");
Date newDate = (Date) old_format.parse(old_date);
DateFormat new_format = new SimpleDateFormat("dd-MMM-yy");
String date = new_format.format(newDate);
System.out.println(date);
You are doing this backwards:
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date newDate = format.parse(date);
DateFormat formatOutput = new SimpleDateFormat("dd-MMM-yyyy");
String output = formatOutput.format(newDate);
But if what you are doing is passing the date to Oracle, you should really use a PreparedStatement
Your date format should be "dd/MM/yyyy"
SimpleDateFormat sdf= new SimpleDateFormat("dd-MM-yy");
DateTime newDate = sdf.format(date);
Try DateFormat format = new SimpleDateFormat("dd/MM/yyyy")
Why don't you try using java.sql.Date.
For example:
Date newDate = new java.sql.Date(date.getTime());
So you can just give an generic sql object for date.
The Date class of Java is quite difficult to use in many cases. I recommend the use of the much better Joda Time classes:
DateTimeFormatter oldFormat = DateTimeFormat.forPattern("dd/mm/yyyy");
DateTimeFormatter newFormat = DateTimeFormat.forPattern("dd-MMM-yy");
DateTime dateTime = oldFormat.parseDateTime("24/04/2012");
String newDate = newFormat.print(dateTime);