I accept the user input date in yyyy/mm/dd format. When I try to convert using SimpleDateFormat it shows "null";
my code for conversion is:
SimpleDateFormat dateofbirthFormat = new SimpleDateFormat("yyyy/mm/dd");
java.util.Date dateOfBirth = dateofbirthFormat.parse(birthdate);
why is it showing null?
MM not mm,
MM is for months,
mm is for minutes
SimpleDateFormat dateofbirthFormat = new SimpleDateFormat("yyyy/MM/dd");
try {
java.util.Date dateOfBirth = dateofbirthFormat.parse("1986/12/11");
System.out.println(dateOfBirth);
} catch (ParseException e) {
}
By yyyy/mm/dd I guest you mean years/Months/days. So you may try using
yyyy/MM/dd (note upcase use of MM).
SimpleDateFormat dateofbirthFormat = new SimpleDateFormat("yyyy/MM/dd");
java.util.Date dateOfBirth = dateofbirthFormat.parse(birthdate); .
Related
I'm trying to create a Date from a String I receive from the server. The String is:
2018-05-23T06:39:37+0000
So the correct format should be:
yyyy-MM-dd'T'HH:mm:ss.SSSZ
Here is my code:
String createdDate = comment.getCreatedDateTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
try {
Date parsedDate = simpleDateFormat.parse(createdDate);
createdDate = parsedDate.toString();
} catch (ParseException ex) {
ex.printStackTrace();
}
mCommentDate.setText(createdDate);
I don't know if there is any way to do this, because after that I would like to parse again to the next format:
dd/MM/yyyy hh:mm
I've tried to parse the original String using this last format directly but I'm getting the same exception.
Any suggestion?
I see you've solved your own problem with a little help from the comments, however I would suggest you seriously consider LocalDate, as the older Date classes are quite troublesome at times.
In fact, as your incoming value has a TimeZone, you'll need to use ZonedDateTime to parse your input.
String createdDate = "2018-05-23T06:39:37+0000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
ZonedDateTime localDate = ZonedDateTime.parse(createdDate, formatter);
System.out.println(localDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")));
Output:
23/05/2018 06:39
The given input date String format
2018-05-23T06:39:37+0000
is incorrect so that you are getting ParseException since millisecond(SSS) part is missing from your date format yyyy-MM-dd'T'HH:mm:ss.SSSZ
So please try with
2018-05-23T06:39:37.235-0530
so below code should work
String createdDate = comment.getCreatedDateTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
try {
Date parsedDate = simpleDateFormat.parse(createdDate);
createdDate = parsedDate.toString();
System.out.println(parsedDate.toString());
} catch (ParseException ex) {
ex.printStackTrace();
}
mCommentDate.setText(createdDate);
Ok, the first mistake (as you've pointed) is I didn't have milliseconds on the original String.
After removing "SSS" from the simpleDateFormat it works like a charm. So this is the final code:
String createdDate = comment.getCreatedDateTime();
SimpleDateFormat defaultDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.getDefault());
try {
Date parsedDate = defaultDateFormat.parse(createdDate);
SimpleDateFormat finalDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.getDefault());
createdDate = finalDateFormat.format(parsedDate);
} catch (ParseException ex) {
ex.printStackTrace();
}
mCommentDate.setText(createdDate);
I am getting a date in the form of dd.mm.yyyy and want to save it as a proper object. It should be comparable to another date object. How do I realize this?
Use SimpleDateFormat as this:
Date date = new SimpleDateFormat("dd/MM/yyyy").parse("12/05/2015");
Using JodaTime
String input = "03.01.2015";
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd.MM.yyyy");
DateTime dt = DateTime.parse(input, formatter);
DateTime now = new DateTime();
System.out.println(dt.compareTo(now));
Use SimleDateFormat
String string = "03.01.2015";
DateFormat format = new SimpleDateFormat("MM.dd.yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);
Try this code
try {
SimpleDateFormat sdf= new SimpleDateFormat("dd.MM.yyyy");
Date d = sdf.parse("19.05.2090");
System.out.println(d);
} catch (ParseException ex) {
ex.printStackTrace();
}
I have to compare two dates whose format is yyyy-MM-dd HH:mm:ss. I know the way to compare date only the before or after date function.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDateandTime = sdf.format(new Date());
String expiryTime = "2014-09-10 00:00:00";
But what's the best way to compare date and time with the current date and time.
Like we have two dates 2014-09-10 00:00:00 and current date with time is 2014-08-31 10:37:15. And now we have to compare it. How we can do that.
Any help is appreciated.
Convert the Date String to java.util.Date object using SimpleDateFormat and compare those date objects with Date#after or Date#before methods.
In java 8 - using new Java Time API, parse date String using DateTimeFormat and get LocalDate object and compare them.
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
final LocalDate dt1 = dtf.parseLocalDate(dateString1);
final LocalDate dt2 = dtf.parseLocalDate(dateString2);
final boolean check = dt1.isAfter(dt2);
if(check)
System.out.println(dt1 +" is after "+dt2);
else
System.out.println(dt2 +" is after "+dt1);
If I understand what you're trying to do you want to use a SimpleDateFormat (and you posted a good pattern) to parse the String(s) into Date(s) and then Date.before(Date). Putting that together into something like,
DateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String firstStr = "2014-09-10 00:00:00";
String secondStr = "2014-08-31 10:37:15";
Date first = sdf.parse(firstStr);
Date second = sdf.parse(secondStr);
boolean before = (first.before(second));
System.out.printf("%s is before %s",
before ? firstStr : secondStr,
before ? secondStr : firstStr);
Output is
2014-08-31 10:37:15 is before 2014-09-10 00:00:00
try{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} catch (ParseException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
String str1 = "2014-09-10 00:00:00";
Date date1 = formatter.parse(str1);
String str2 = "2014-08-31 10:37:15";
Date date2 = formatter.parse(str2);
if (date1.compareTo(date2)<0)
{
System.out.println("date2 is Greater than my date1");
}
This question already has answers here:
convert string into date format
(5 answers)
Closed 9 years ago.
Hello i have date in this format 2013-10-31T19:00:00Z now i want to display this date into yyyy/mm/dd this format.i have used this code but its giving me exception kindly help me
String startDateString = "2013-10-31T19:00:00Z";
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
Date startDate;
try {
startDate = df.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println("Date is "+newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
Try this
String DateStr="2013-10-31T19:00:00Z";
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
Date d = new SimpleDateFormat("yyyy-MM-dd").parse(DateStr);
System.out.println(sim.format(d));
output 2013-10-31
That's because you're specifically telling Java to look for a 4-digit year followed by a 2-digit month and 2-digit date. Taken straight from the docs, what you want is
"yyyy-MM-dd'T'HH:mm:ssZ"
You can use SimpleDateFormat's parse method to parse your date, and then use the format method to output it in the format that you prefer.
Try this:
String startDateString = "2013-10-31T19:00:00Z";
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
DateFormat outputFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = inputFormat.parse(startDateString);
System.out.println("Date is " + outputFormat.format(date));
Output:
Date is 2013/10/31
You need a separate SimpleDateFormat to parse the Datestring. Try this:
String startDateString = "2013-10-31T19:00:00Z";
DateFormat originalFormat = new SimpleDateFormat("yyyy-mm-dd'T'HH:MM:SS'Z'");
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
Date startDate;
try {
startDate = originalFormat.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println("Date is " + newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
You need two different DateFormat. one for parse date and one for formatting date.
String startDateString = "2013-10-31T19:00:00Z";
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:SS'Z'");
Date startDate;
try {
startDate =df1.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println("Date is "+newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "07/06/2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Here is simple example
Try this:
try {
String startDateString = "2013-10-31T19:00:00Z";
DateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
DateFormat output = new SimpleDateFormat("yyyy/MM/dd");
Date date = input.parse(startDateString);
System.out.println("Date is " + output.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Pointing an another simple mistake in your code
You coded as DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
here mm is for minutes , for month use MM
I am trying to conver the string to a date
String date = "12/31/2012";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
try {
Date parse = sdf.parse(date);
System.out.println(parse);
} catch (ParseException ex) {
ex.printStackTrace();
}
But it appears to me that it is having an Exception. of ParseExpception. why is that? I want to generate a date for 12/31/2012
Date String - 12/31/2012
It matches with - MM/dd/yyyy
d - Day of the month
M - Month in year
y - Year
...
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
format for month is MM not mm and also your should should delimit with - and not / as your formatter delimiter is / and your format should match your date atring.
date 12-31-2012
fmt MM-dd-yyyy
String date = "12-31-2012";
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
If You could use Joda time
String dateStr = "11/15/2013";
Date date = DateTimeFormat.forPattern("dd/MM/yyyy").parseDateTime(dateStr).toDate();