I want to convert the string 11-7-2013 10:51:10 to a Date object.
formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
try {
String date = "11-7-2013 10:51:10"
return formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
I have the following code, But I'm getting a ParseException.
Try
formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
Use
formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
try {
String date = "11-7-2013 10:51:10"
return formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
In SimpleDateFormat the string passed to constructor is the same representation of date format that date is going to parse by this formatter.
formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
instanse of
formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
You need to use "-" instead of "/" in your date format as your date string contains "-" ,
formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
try {
String date = "11-7-2013 10:51:10"
return formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
There are two ways here
"dd/MM/yyyy hh:mm:ss" with String Date ="11/7/2013 10:51:10";
or
"dd-MM-yyyy hh:mm:ss" with String Date="11-7-2013 10:51:10";
please refer the below format
tring string = "January 2, 2010";
Date date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(string);
Related
My string date --> 2016-10-02T00:00:00.000Z. I want to get only date from this string. I tried to parse through below coding but it throws me error! I have exactly the same format as mentioned in the string. Any answers?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
try {
Date myDate = sdf.parse( dateofJoining.replaceAll( "([0-9\\-T]+:[0-9]{2}:[0-9.+]+):([0-9]{2})", "$1$2" ) );
System.out.println("Date only"+ myDate );
} catch (ParseException e) {
e.printStackTrace();
}
I also tired below code,
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = format.parse(dtStart);
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The error which i get
java.text.ParseException: Unparseable date: "2016-10-02T00:00:00.000Z" (at offset 19)
05-12 00:18:36.613 4330-4330/com.vroom.riderb2b W/System.err: at java.text.DateFormat.parse
change the simple date format to use: yyyy-MM-dd'T'HH:mm:ss.SSSZ
in your code:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
try {
Date date = format.parse(dtStart.replaceAll("Z$", "+0000"));
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
If you want to get date/mm/yy from it:
use:
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
// use UTC as timezone
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Log.i("DATE", sdf.format(date)); //previous date object parsed
if you want output format: hour:minute AM/PM
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(sdf.format(date));
EDIT
More easier option is to split the string in two parts like:
String dateString = "2016-10-02T00:00:00.000Z";
String[] separated = dateString.split("T");
separated[0]; // this will contain "2016-10-02"
separated[1]; // this will contain "00:00:00.000Z"
try with this :
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Try
Calendar calendar = Calendar.getInstance();
TimeZone localTZ = calendar.getTimeZone();
String format1 = "yyyy-MM-dd"; //will return 2017-01-31
String format2 = "dd"; //will return DAY only like 31
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setTimeZone(localTZ );
String result = sdf.format(your_date);
For me it's worked like this:
textView_last_comm.setText(parseDateFormat(passDetailsModel.getLast_comm(), "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "dd/MM/yy HH:mm"));
public static String parseDateFormat(String dateToFormat, String inputFormat, String outputFormat) {
SimpleDateFormat inputFormat = new SimpleDateFormat(inputFormat);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputFormat);
Date date = null;
String str = null;
try {
date = inputFormat.parse(dateToFormat);
str = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return str;
}
You can get date easily by using String.substring() method:
String string = "2016-10-02T00:00:00.000Z";
String date = string.substring(0, 10);
Log.d("SUCCESS", "DATE: " + date);
OUTPUT:
D/SUCCESS: DATE: 2016-10-02
Response from jsp is coming in this format: "mm/dd/yyyy hh:mm", and I want to convert to db format "yyyy-mm-dd hh:mm:ss".
I tried this code :
public java.sql.Date getdateFormat(String datestring) throws ParseException {
String datestr = "";
try {
java.util.Date date = new SimpleDateFormat("mm/dd/yyyy hh:mm a",
Locale.ENGLISH).parse(datestring);
atestr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
} catch (Exception e) {
e.printStackTrace();
}
return getDateFromString(datestr);
}
public java.sql.Date getDateFromString(String string) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
java.util.Date finalDate = null;
try {
finalDate = sdf.parse(string);
} catch (Exception e) {
e.printStackTrace();
}
return new java.sql.Date(finalDate.getTime());
}
A common mistake in using SimpleDateFormat is skip the documentation and assume that is knows when mm means months and when mm mean minutes. It doesn't. mm only means minutes. If you want months use MM Also only use a if you expect AM/PM and only use hh for 12 hour clocks. I would expect your format should read
MM/dd/yyyy HH:mm
and your output
yyyy-MM-dd HH:mm:ss
BTW You shouldn't need to convert to a String to use JDBC. Using a Date is faster and less error prone.
If all you want is the Date then you do not need to do
atestr=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
simply return the date as this stage.
A date does not have any formatting, it is basically a number.
I would basically do it like this
String dateInString = "20140611";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date toDate = sdf.parse(dateInString);
sdf = new SimpleDateFormat("yyyy-MM-dd");
String tmpStr = String.format(sdf.format(toDate));
System.out.println(tmpStr);
We can get the date in the following way
DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parseDate = dfm.parse(datestring);
Now the parseDate is in the format "yyyy-MM-dd HH:mm:ss"
I am trying to convert date from one format to other format but the following code is giving me the exception: please help
public class Formatter {
public static void main(String args[]) {
String date = "12-10-2012";
try {
Date formattedDate = parseDate(date, "MM/dd/yyyy");
System.out.println(formattedDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Date parseDate(String date, String format)
throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(format);
return formatter.parse(date);
}
}
To convert from "MM-dd-yyyy" to "MM/dd/yyyy" you have to do as follows:
SimpleDateFormat format1 = new SimpleDateFormat("MM-dd-yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("MM/dd/yyyy");
Date date = format1.parse("12-10-2012");
System.out.println(format2.format(date));
If you input "12-10-2012" then output will be "12/10/2012":
change slash / to dash -
MM-dd-yyyy instead of MM/dd/yyyy
it should be Date formattedDate = parseDate(date, "MM-dd-yyyy");
Your format uses slashes (/) but the date you supply uses dashes (-). Change to:
Date formattedDate = parseDate(date, "MM-dd-yyyy");
And you should be good :)
Try this.
Date formattedDate = parseDate(date, "MM-dd-yyyy");
String myDate = new String("2011-06-23T00:00:00");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
this.thedate = format.parse(myDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I'm not sure what I'm doing, but I have a timestamp that will be a string and I want to parse out the year and month. This is what I have so far.
You need to use "yyyy-MM-dd'T'HH:mm:ss" as a pattern for the SimpleDateFormat:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
I guess format should be this:
format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
I have record in the file as 17 Dec 2010 17:02:24 17 Dec 2010 18:02:24. I am reading these from file....
my parser code is:
static SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy hh:mm:ss");
public static String DateFormat(String startdate) {
String date = null;
try {
java.util.Date tDate = df.parse(startdate);
df = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");
String formatteddate = df.format(tDate).toUpperCase();
return formatteddate;
} catch (ParseException e) {
System.out.println("Unable to Parse" + e);
}
return date;
}
but only first date format get parsed...then error will be unable to parse
you are over writing the df value again with a different format (as shown below) in the DateFormat(...) method. df is a static variable so it will use this new format for sub sequent reads. Use a new local variable for "dd-MMM-yy hh:mm:ss a"
df = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");
I hope this helps.
static SimpleDateFormat inputDateFormat = new SimpleDateFormat("dd MMM yyyy hh:mm:ss");
static SimpleDateFormat outputDateFormat = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");
public static String getFormattedDate(String startdate) {
String date = null;
try {
java.util.Date tDate = inputDateFormat.parse(startdate);
String formatteddate = outputDateFormat.format(tDate).toUpperCase();
return formatteddate;
} catch (ParseException e) {
System.out.println("Unable to Parse" + e);
}
return date;
}
Your problem is that you're re-using df as Pangea stated.
static SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy hh:mm:ss");
public static String DateFormat(String startdate) {
String date = null;
try {
java.util.Date tDate = df.parse(startdate);
SimpleDateFormat outputDf = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");
String formatteddate = outputDf.format(tDate).toUpperCase();
return formatteddate;
} catch (ParseException e) {
System.out.println("Unable to Parse" + e);
}
return date;
}