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();
Related
String startDateStr = "2017-02-03"
DateFormat formatter = new SimpleDateFormat("yyyy-MM-DD",Locale.US);
Date date = (Date)formatter.parse(startDateStr);
2017-02-03 date is parsed to Tue Jan 03 00:00:00 GMT+05:45 2017
Did I
miss something?
Update
I needed a string to be converted to a date object
while maintaining the same format.
The reason for this is I want to make use of public boolean after(Date when) method
This will work ^_^
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");
String startDateStr ="2017-02-03";
Date date = null;
try {
date = inputFormat.parse(startDateStr);
String startDateStrNewFormat = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
Little explanation of your output :
D is Day in year (1-365)
d is day in month (1-31)
Check the document
Use SimpleDateFormat type for fomatter. You are creating DateFormat object but using SimpleDateFormat.
String startDateStr = "2017-02-03"
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
Date date = (Date)formatter.parse(startDateStr);
Yes you missed something. You used DD instead of dd in your yyyy-MM-DD format string. Here is how you do it:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(new Date());
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");
}
I have a string like this:
"2013-06-25 10:00:09"
I want to format it like this:
"25/06/2013 10:00".
I'm trying this:
SimpleDateFormat sdf = new SimpleDateFormat(""dd/MM/yyyy kk:mm"")
Date d;
try {
d = sdf.parse(data_publicao_db[totalCount_ + i]);
String s2 = (new SimpleDateFormat("yyyyMMdd")).format(d);
Log.d("Data test", "" + s2);
}
catch (ParseException e) { e.printStackTrace(); }
But it keeps giving a
ParseException: Unparseable date
Is there a better and right way to this?
sdf should be
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
the other SimpleDateFormat you need to reformat your date:
new SimpleDateFormat("dd/MM/yyyy HH:mm")
Let's go!
//your date String and SDF in your String format
String originalDateString = "2013-06-25 10:00:09";
SimpleDateFormat originalSdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//get the Date from the String
Date date = originalSdf.parse(originalDateString);
//Wanted output format
SimpleDateFormat wantedSdf= new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//print it
System.out.println(wantedSdf.format(date));
Since data_publicao_db[totalCount_ + i] is looking like 2013-06-25 10:00:09, you have to parse it with a format of yyyy-MM-dd HH:mm:ss .
To convert the result to the format 25/06/2013 10:00, you have to format it with a SimpleDateFormat using the format dd/MM/yyyy HH:mm.
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); .
I have my date and time stored in string i.e my string contains str ="18/01/2013 5:00:00 pm". How can I convert it to 24 format time in android?
You can use two SimpleDateFormat instances: one to parse the input as a date and a second to format the date as a string with the desired format.
For example, formattedDate in the code below will be 18/01/2013 17:00:00:
String str = "18/01/2013 5:00:00 pm";
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date dt = input.parse(str);
SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String formattedDate = output.format(dt); //contains 18/01/2013 17:00:00
Notes:
hh is for Hour in am/pm (1-12) whereas HH is for Hour in day (0-23).
for more formatting options, check the javadoc
try
String str ="18/01/2013 5:00:00 pm";
Date date = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a").parse(str);
str = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(date);
System.out.println(str);
output
18/01/2013 17:00:00
To get AM PM and 12 hour date format use hh:mm:ss a as string formatter WHERE hh is for 12 hour format and a is for AM PM format.
Note: HH is for 24 hour and hh is for 12 hour date format
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
Example
String date = "18/01/2013 5:00:00 pm";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
You can use SimpleDateFormat for that:
SimpleDateFormat dateFormat = new SimpleDateFormat(dd/mm/yyyy HH:mm:ss");
Refer to this which was opposite to your requirement. Just posted the link so you might get the idea of difference that HH and hh makes.
Try using the java SimpleDateFormat class.
Example:
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
df.parse(date);
The upper case HH use a 24h format