change date format DD-MON-YYYY to DD/MM/YYYY - java

I want to change date format which I received reading from excel cell file is "30-mar-2016" to 03/30/2016. I have tried below
String inputDate = "30-mar-2016";
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Date startDate;
startDate = df.parse(inputDate);
It gave me- java.text.ParseException: Unparseable date: "30-mar-2016"
.
I also tried below code
String inputDate = "30-mar-2016";
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
String startDate;
startDate = df.format(inputDate);
getting -- java.lang.IllegalArgumentException: Cannot format given Object as a Date
Can anyone help me .

Your input string 30-mar-2016 is in the format dd-MMM-yyyy. Your output format is MM/dd/yyyy. So you need two DateForamts. One for parsing original input string, one for formatting output string.
DateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy"); // for parsing input
DateFormat df2 = new SimpleDateFormat("MM/dd/yyyy"); // for formatting output
String inputDate = "30-mar-2016";
Date d = df1.parse(inputDate);
String outputDate = df2.format(d); // => "03/30/2016"

I have resolved using below
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
Date date = sdf.parse("30-mar-2016");
sdf.applyPattern("MM/dd/yyyy");
System.out.println(sdf.format(date));//got 03/30/2016
Thanks everyone for your quick response

Related

How to format the given date "31.12.9999" in the format "yyyy/MM/dd HH:mm:ss"

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 have a Date in String 20090422 110126000 format yyyymmdd hh24miss 20090422 110126000

I have a Date in String in gwt.
This date is stored as String in oracle database format is
//format yyyymmdd hh24miss.
On java/gwt side I want to change the format to yyyy/mm/dd hh:mm:ss.SSS
String dateString = '20090422 110126000' //format yyyymmdd hh24miss.
How to convert it into this format yyyy/mm/dd hh:mm:ss.SSS.
Im not been able to find the correct format to parse the string in Date.
You can try it using this date format, like
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd hhmmssSSS");
System.out.println(dateFormat.parse("20090422 110126000"));
Which will give you this output
Wed Apr 22 11:01:26 CEST 2009
You can use GWT DateTimeFormat class to parse any String and to format the resulting Date.
//From String to Date
String dateString = '20090422 110126000'
DateFormat format = new SimpleDateFormat("yyyyMMdd hhmmssSS");
Date date = format.parse(dateString);
//From Date to String
DateFormat df = new SimpleDateFormat("yyyy/mm/dd hh:mm:ss.SSS");
String finalDate = df.format(date);

How to parse String "24-10-2012" into Date ( dd-mm-yyyy) format?

I am using jdk- 1.6.
I am try to parse String "24-10-2012" date to Date (24-10-2012) but i am getting this error:
java.text.ParseException: Unparseable date: "18-11-2012"
java.text.DateFormat.parse(DateFormat.java:354)
I am parsing like this:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
System.out.println(formatter.parse(currentDate));
prints
Wed Oct 24 00:00:00 CEST 2012
Your problem cannot be reproduced with the code you have posted.
My hypothesis: your exception is thrown from a piece of code other than the one you are accusing of the error. You could try carefully analyzing the stack trace in order to track down the real culprit.
Date in java does not hold any format. Read more...
When I run
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
System.out.println(date);
System.out.println(formatter.format(date));
I get
Wed Oct 24 00:00:00 BST 2012
24-10-2012
which is as I expected. Can you clarity what the problem is?
You can use this for the format "dd-mm-yyyy"
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
System.out.println(formatter.format(date));
import java.util.Date;
import java.text.SimpleDateFormat;
public class SimpleFormatDate
{
public static void main(String args[]){
Date todaysDate = new java.util.Date();
// Formatting date into yyyy-MM-dd HH:mm:ss e.g 2008-10-10 11:21:10
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
// Formatting date into yyyy-MM-dd e.g 2008-10-10
formatter = new SimpleDateFormat("yyyy-MM-dd");
formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
// Formatting date into MM/dd/yyyy e.g 10/10/2008
formatter = new SimpleDateFormat("MM/dd/yyyy");
formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
}
}
output
Formatted date is ==>2008-10-10 13:03:54
Formatted date is ==>2008-10-10
Formatted date is ==>10/10/2008
Wait a second.. Why u need to parsing that if u have a right value ?
Anyway, i use this :
SimpleDateFormat oFormat = new SimpleDateFormat("yyyy-MM-dd");
String sDate = oFormat.format("24-10-2012");
it will appearing date like 2012-10-24. So if u want to parsing to dd-MM-yyyy, u just need change the format to what u want.
NB : Sorry if my english is bad. :D

Get time from entire date

I have to get time from entire date
e.g. time=11:00:00 from date 2012-09-01 11:00:00.0
I tried following snippet but getting error Error : Unparseable date: "2012-9-1.13.30. 0. 0"
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = inputFormat.parse(iResultSet1.getString(i));
DateFormat outputFormat = new SimpleDateFormat("hh:mm:ss");
String outputString = outputFormat.format(date);
Edit: Now I am getting only date instead I want only time
if (iResultSet1.getDate(i) != null) {
Date date = iResultSet1.getDate(i);
System.out.println("date-->" + date);
// Format date into output format
DateFormat outputFormat = new SimpleDateFormat(
"HH:mm:ss");
String outputString = outputFormat.format(date);
// System.out.println("date1-->"+date1);
I wil suggest, in this case rather doing parsing and manipulation in java change your SQL to format and return only date as
Example
SELECT TIME_FORMAT(NOW(), '%H:%i:%s');
You are probably retrieving your date from the database (iResultSet1.getString(i)) and the problem is that you're getting wrong format, i.e. 2012-9-1.13.30. 0. 0. Either change the date format in the database or use:
Date date = iResultSet1.getDate(i);
instead of
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = inputFormat.parse(iResultSet1.getString(i));

DateFormat Parsing Error

I have a date coming in. So for example 02/16/2012. What I need to do in order to work with my database format is convert it to 2012-02-16. I figured this would be pretty straightforward. But I can't work it. Here is my date parser.
String endDateString = "02/16/2012"
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
endDate = dateFormat.parse(endDateString)
Any suggestions? Thanks.
What you want to do is parse the incoming format to a Date object
final SimpleDateFormat idf = new SimpleDateFormat("MM/dd/yyyy");
final Date indate = idf.parse("02/16/2012");
then reformat the Date object to the format you desire
final SimpleDateFormat odf = new SimpleDateFormat("yyyy-MM-dd");
final String s = odf.format(indate);
You should parse it FROM one correct format, then convert TO string with another correct format. What are you doing -- is trying to interpret date in MM/dd/yyyy format with yyyy-MM-dd format, i.e. incorrect.
SAMPLE
public static void main(String[] args) throws ParseException {
SimpleDateFormat sourceFormat = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat destinationFormat = new SimpleDateFormat("yyyy-MM-dd");
String sourceDateString = "02/16/2012";
String destinationDateString;
Date dat; // this object stores "perfect" date object
// interpreting string with input format and converting it to date
dat = sourceFormat.parse(sourceDateString);
// expressing date object as string in destination format
destinationDateString = destinationFormat.format(dat);
System.out.format("Done from %s to %s\n", sourceDateString, destinationDateString));
}
The date-format you specify has to match the date-format of your input like so:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
This snippet can tell you how to get it done.
SimpleDateFormat inputDateFormat = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String endDateString = "02/16/2012";
String endDate = outputDateFormat.format(inputDateFormat.parse(endDateString));
Reference: SimpleDateFormat
You can also convert a date at the database level. For example MySQL has a function STR_TO_DATE. In your case it would be STR_TO_DATE('02/16/2012','%m/%d/%Y')
You are parsing with wrong format
new SimpleDateFormat("MM/dd/yyyy");

Categories

Resources