how to convert string into date? JAVA - java

I have this string: 7 -Jun- 2014.
I want to convert to java.utils.Date;
I use this Code
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "7-Jun-2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
but I get this exception :
java.text.ParseException: Unparseable date: "7-Jun-2013"
at java.text.DateFormat.parse(Unknown Source)
at ma.abcsolution.util.Test.main(Test.java:15)

try using
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
the SimpleDateFormat look at the given string like it you tell it to, so in your example it look for a string with 2 chars for days, followed by a '/' sign and then 2 chars for month and so on

Using SimpleDateFormatter you can convert from date string to date and date to date String
public final class DateUtil {
private static final String DEFAULT_DATE_FORMAT = "dd-MMM-yyyy";
private DateUtil() {
}
public static final String formatDate(final Date date, final String dateFormat) {
DateFormat format = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
return format.format(date);
}
public static final Date formatDate(final String date) throws ParseException {
return formatDate(date, DEFAULT_DATE_FORMAT);
}
public static final Date formatDate(final String date, final String dateFormat) throws ParseException {
DateFormat format = new SimpleDateFormat(dateFormat);
return format.parse(date);
}
}
Whenever you are going yo format date please verify the format you are using
In your case you used dd/MM/yyyy but date you used in format dd-MMM-yyyy.

Use
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
to format the date. You used the wrong format with the / signs.
See also:
http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Date date = new SimpleDateFormat("d-MMM-yyyy").parse("7-Jun-2014");

Use this code. It is simple.
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTime {
public static void main(String args[]){
SimpleDateFormat ft = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "7-Jun-2013";
Date date=new Date(dateInString);
System.out.println(ft.format(date));
}
}

Related

Java SimpleDateFormat Cannot format given Object as a Date

I am trying to convert string to date using date format and used the following code but its shows an error.
public static Date ConvertStringtodate(String Date) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date Teststart = dateFormat.parse(Date);
return Teststart;
}
public static void main(String[]agrs) throws ParseException {
System.out.println(ConvertStringtodate("2022.02.10 17:54:55"));
}
Here is the error
Exception in thread "main" java.lang.IllegalArgumentException: Cannot
format given Object as a Date at
java.text.DateFormat.format(DateFormat.java:310) at
java.text.Format.format(Format.java:157)
At the main method, you are sending date as "2022.02.10 17:54:55". However, you wrote format of the pattern as "yyyy-MM-dd hh:mm:ss". Change the pattern at the SimpleDateFormat constructor as "yyyy.MM.dd HH:mm:ss".
The problem for me was the slashes '/' in the date input. for some reason the. The input string was "01/01/1991" instead of "01-01-1991". So I just replaced the slashes with dashes and everything worked just fine.
private Date convertStringToDate(String payload) throws ParseException {
payload = payload.replace("/", "-");
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
java.util.Date utilDate = formatter.parse(payload);
return new Date(utilDate.getTime());
}

Converting of string type to date parse not working

I am converting the string type to date and parsing it to a HH:mm format but while formatting its not working throwing exception,campusconfig.getworktime() is date Below is my code
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
if ((from1 != null)) {
if(campusConfig.getWorkFromTime()!=null){
String time = "";
Date timech;
time = campusConfig.getWorkFromTime().toString();
timech = dateFormat.parse(time);
if(from1.before(timech)){
String errormsg = message.getString("message.reports.time.select.campustime");
errorMessageUI(errormsg);
}
If campusConfig contains a full date (date and time) you need to parse it with SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSSSSS") (with the correct format) then you can trasform the date in only time with other SimpleDateFormat("HH:mm") format(date).
Sample:
public class DateFormat {
public static void main(String[] args) throws ParseException {
String campusConfig = "2015-11-26 11:46:20";
SimpleDateFormat sdf_in = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf_in.parse(campusConfig);
SimpleDateFormat sdf_out = new SimpleDateFormat("HH:mm");
System.out.println("Only Time : " + sdf_out.format(d));
}
}
try something like below,
new SimpleDateFormat("HH:mm").format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(campusConfig.getWorkFromTime().toString()));
try this
java.util.Date temp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").parse("2012-07-10 14:58:00.000000");

Encountering issue in SimpleDateFormat parse method in Java

I am trying to use SimpleDateFormat.parse method to parse a date string to Date object, but it is omitting "T" in the final date that is returned. I am passing this date string 2015-04-15T12:55:07.365 and I am getting 2015-04-15 12:55:07.365 in the output. However, the desired output is 2015-04-15T12:55:07.365.
Why is "T" in the final output omitted by this line parsedDate = sdf.parse(transDate);
public static void main(String[] args)
{
try
{
final String pattern = "yyyy-MM-dd'T'hh:mm:ss.SSS"; // example 2015-04-15T12:55:07.365
final SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String transDate = "2015-04-15T12:55:07.365";
Date parsedDate = sdf.parse(transDate);
System.out.println("transDate:"+transDate+", parsedDate: "+parsedDate);
}
You never get your desired output 2015-04-15T12:55:07.365
Why?
Because you are printing Date object parsedDate.Date class has it's own toString() method implementation.When you are printing the date object, it means it basically prints the toString() method implementation format.
see the Java doc for details
System.out.println(parsedDate) would give you Wed Apr 15 00:55:07 GMT 2015 which is the toString() representation of the date object.
You can use SimpleDateFormat to parse AND format dates:
SimpleDateFormat sdfParser = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
Date date = sdfParser.parse("2015-04-15T12:55:07.365");
SimpleDateFormat sdfFormatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
String formattedDate = sdfFormatter.format(date);
System.out.println(formattedDate);
// 2015-04-15T12:55:07.365
You will get desired output here.
public static void main(String args[]) {
{
try {
String transDate = "2015-04-15T12:55:07.365";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
Date date = sdf.parse(transDate);
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date d = sdf.parse(sdf.format(date));
String formattedTime = output.format(d);
System.out.println("transDate:" + transDate + ", parsedDate: " + formattedTime);
} catch (Exception e) {
}
}
}

java.lang.IllegalArgumentException: Cannot format given Object as a Date

I recive a date in this format
2014-12-09 02:18:38
which i need to convert it to
09-12-2014 02:18:38
I tried converting this way
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class TestDate {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-YYYY HH:mm:ss");
String input = "2014-12-09 02:18:38";
String strDate = sdf.format(input);
System.out.println(strDate);
}
}
But i am getting this exception during Runtime
Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
at java.text.DateFormat.format(DateFormat.java:301)
at java.text.Format.format(Format.java:157)
at TestDate.main(TestDate.java:15)
Could anybody please help me how to resolve this .
Try this
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdfOut = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String input = "2014-12-09 02:18:38";
Date date = sdfIn.parse(input);
System.out.println(sdfOut.format(date));
}
Also, note that m is for minutes, while M is for months.
Instead of
String strDate = sdf.format(input);
use
String strDate = sdf.parse(input);
Also see this question
SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-YYYY HH:mm:ss");
// String to Date:
String input = "2014-12-09 02:18:38";
Date date = sdf.parse(input);
// Date to String:
String strDate = sdf.format(date);
System.out.println(strDate);
SimpleDateFormat in can be used to format a Date object.
First you have to parse your string to a Data object using format on the SimpleDateFormat you declared.
After that you can output it using a second SimpleDataFromat to a string you want.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String input = "2014-12-09 02:18:38";
Date dt = sdf.parse(input);
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String strDate = sdf2.format(dt);
System.out.println(strDate);

How to convert String of datetime to date using GWT?

In mysql, i have a field time_entered of type datetime (sample data: 2012-06-20 16:00:47). I also have a method, getTimeEntered(), that returns the value as String. I want to display the date in this format 2012-06-20 using DateTimeFormat from GWT.
here's my code:
String date = aprHeaderDW.getTimeEntered();
DateTimeFormat fmt = DateTimeFormat.getFormat("MM-dd-yyyy");
dateEntered.setText("" + fmt.format(date));
The problem is, the format method doesn't accept arguments as String. So if there's only a way I could convert the date from String to Date type, it could probably work. I tried typecasting but didn't work.
You should be able to just use DateTimeFormat.
Date date = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss").parse("2012-06-20 16:00:47");
String dateString = DateTimeFormat.getFormat("yyyy-MM-dd").format(date);
Otherwise there is a light-weight version of SimpleDateFormat that supports this pattern.
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-06-20 16:00:47");
Hi There are two options.
The first is as it is already a string you could use a regular expression to modify the format.
The second is using a SimpleDateFormater you can parse the string to a date then back again.
For example:
public class DateMerge {
public static void main(String arg[])
{
String out = dateConvert("2012-06-20 16:00:47");
System.out.println(out);
}
public static String dateConvert (String inDate)
{
try {
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = (Date)formatter.parse(inDate);
formatter = new SimpleDateFormat("dd-MM-yyyy");
String outDate = formatter.format(date);
return outDate;
} catch (ParseException e)
{System.out.println("Exception :"+e); }
return null;
}
}
You may use like this.
String date = "2012-06-20 16:00:47";
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
String lDate=sf.format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date));
System.out.println(lDate);
Output:
2012-06-20
After trying a lot of times I came up with a solution, based on #Keppil and adding my own code.
Here's Keppil's suggested solution for converting String datetime into Date type:
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-06-20 16:00:47");
...but my second requirement is to display just the date like 2012-06-20. Even though I removed HH:mm:ss, it still displayed the time like this 2012-06-20 00:00:00.
Here's my final solution:
Date date = null;
String d = rs.getString(SQL_CREATION_TIME); // assigns datetime value from mysql
// parse String datetime to Date
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(d);
System.out.println("time entered: "+ date);
} catch (ParseException e) { e.printStackTrace(); }
// format the Date object then assigns to String
Format formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd");
String s = formatter.format(date);

Categories