Formatting a date String - java

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.

Related

Parse yyyy-MM-DD String to Date yyyy-MM-DD in Android?

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());

How to get proper written date using SimpleDateFormat in Java

I have this:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date result = dateFormat.parse(this.getCreatedTime());
Basically I want to convert a string like "2016-09-27T09:19:57Z" into something like "September 27, 2016 at 9:19 AM".
If I use the code above I end up with a Date object, but all the methods are deprecated. So how do I achieve this?
You can use DateFormat again as #Thomas wrote:
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date inputDate = inputFormat.parse(this.getCreatedTime());
DateFormat outputFormat = new SimpleDateFormat("outputFormat");
String output = outputFormat.format(inputDate);
You should do research before posting any question here.
Use this to get Date from your required pattern
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
Date date = null;
try {
date = format.parse(unformattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Make new instance of your desired patter.
format = new SimpleDateFormat("MMMM dd, yyyy 'at' HH:mm a", Locale.getDefault());
String formattedDate = format.format(date);

Compare two dates with time whose Formate is yyyy-MM-dd HH:mm:ss in Android

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");
}

Java Converting String(day/month/year) format to Date

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();

Cannot parse String into Date from Json, UnparsableDateException

I have used this code to parse a String received from Json in Date Format.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
Date myDateConference = sdf.parse(jsonO.getString("myDateConference"));
}
catch(ParseException e){
e.printStackTrace();
}
The String of the date is something in this form 2012-08-02T00:00:00
But I get this exception
java.text.ParseException: Unparseable date: "2012-08-02T00:00:00"
What is wrong?
use
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
instead of
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
There is a T in your source String:
"2012-08-02<b>T</b>00:00:00"
Change the format to
yyyy-MM-dd'T'HH:mm:ss

Categories