Android change the format of a string representing time and date - java

I have the string in the format ss:mm:hh dd/mm/yyyy as for example 58:21:16 20/07/2012 but i want the string to be in the format as dd/mmm/yyyy hh:mm:ss as for example 19/Mar/2012 17:34:58.
But i am getting format as 20:040:2012 02:40:53.
Here the string i am passing is static.
Anybody having any sort of idea regarding this can help.Any sort of help is appreciated.

Use a capital M for month.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy hh:mm:ss");
textView.setText(sdf.format(new Date()));
M stands for month and m for minute.
Reference

Related

Oneliner to get a string representation of Day of week, day of month and time according to default locale

I'm using a combination of DateFormat and SimpleDateFormat to achive following string from a Date object:
fre 20 22:48
This snippet produces the outcome above:
DateFormat localizedTimeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
String str = new SimpleDateFormat("EE d", Locale.getDefault()).format(date) + " " + localizedTimeFormatter.format(date);
The outcome is exactly as I want, but I think there are improvements that could be done. I would like to use only the SimpleDateFormat to achieve this and getting shortname for day in week and day of month are not the problem. The problem is getting time in only hours and minutes according to locale, or more correctly, 12 or 24 hour format. I've checked out the documentation on the SimpleDateFormat Javadoc SimpleDateFormat
but as I can see there are no way of getting a time(hhmm) in 12 or 24hour format just by setting a format string to the SimpleDateFormat? Are there any way to achieve this by only using SimpleDateFormat as a oneliner, or do I have to do it the way I have done already, or are there a completely different way of solving this little issue?
How about
SimpleDateFormat sdf = new SimpleDateFormat("EE d HH:mm", Locale.getDefault());
or
SimpleDateFormat sdf = new SimpleDateFormat("EE d hh:mm", Locale.getDefault());
Your method is on the right track.
However, I didn't need to use the DateFormatter.
I got your expected result in this line itself.
String str = new SimpleDateFormat("EEE, d, k:m", Locale.getDefault()).format(Calendar.getInstance().getTime());
Output is:
Mon, 30, 19:5
The pattern you use in the first argument to SimpleDateFormat is the key here I think.
The meaning of the individual alphabets and their combination is already mentioned in the link you gave.
Hope this helps.

SimpleDateFormat parsing date string incorrectly

I'm trying to parse a String into a Date and then format that Date into a different String format for outputting.
My date formatting code is as follows:
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat dateParser = new SimpleDateFormat("d/M/yyyy h:m:s a");
String formattedDocumentDate = dateFormatter.format(dateParser.parse(sysObj.getString("document_date")));
The result of sysObj.getString("document_date") is 1/31/2013 12:00:01 AM. And when I check the value of formattedDocumentDate I get 01/07/2015.
Any help is much appreciated.
You are parsing days 31 as months. SimpleDateFormat tries to give you a valid date. Therefore It adds to 1/0/2013 31 months. This is 2 years and 7 month. So you get your result 01/07/2015. So SimpleDateFormat works correct.
One solution for you is to change your date pattern to M/d/yyyy h:m:s a or your input data.
To avoid these tries you have to switch off SimpleDateFormat lenient mode. Then you will get an exception if the format does not fit.
It looks like your input format is actually months first, then days. So should be "MM/dd/yyyy".
So:
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/M/yyyy");
SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String formattedDocumentDate = dateFormatter.format(dateParser.parse(sysObj.getString("document_date")));
Another example like this
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyyMMdd");
System.out.println("date is:"+new java.sql.Date( sdfInput.parse("20164129").getTime() ));
Output is: 2019-05-29
I expect to throw parse exception but not (41)is not a valid month value.
on the other hand if I gave 20170229, system can recognize the February of 2017 doesn't have a lap year and return 2017-03-01 interesting.

Converting string into date type

I have used the Calendar class to get the current Date. Now I want to convert that date to Date format so that it can be stored in database with format "yyyy.mm.dd". I tried to convert this using SimpleDateFormat class
String dateString = dateText.getText();
SimpleDateFormat dateFormat = new SimpleDateFormat(" yyyy.mm.dd ");
Date convertedDate = dateFormat.parse(dateString);
but I couldn't convert into Date type.
Try to remove spaces from the format string
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.mm.dd");
Also if your input date has invalid format you might get a parse exception. Better if you put it into try/catch block.
Notice, that m stands for minute in hour but M for month of year. Make sure you put a valid format pattern.
You havent stated what the error is but its unlikely that you want to use a minute field to parse the month. Use uppercase M:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
get rid of the whitespaces in your pattern

Getting formatted date in date format rather than String

I'm running the program written below, but instead of printing in mm/dd/yyyy hh:mm format it prints in the normal date format(ie. Day Date and time)
SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy hh:mm");
Date date = sdf.parse(sdf.format(Calendar.getInstance().getTime()));
The reason i'm doing this is because the existing method accepts parameters in Date format, so i need to send the above mentioned date object to it.
Please point out the mistake or suggest some other alternative.
Thanks
Date objects don't have a format. The Date class is a wrapper around a single long, the number of milliseconds since the epoch. You can't "format" a Date, only a String. Pass around a Date/Calendar internally, and format it whenever you need to display it, log it, or otherwise return it to the user.
Change the format to MM/dd/yyyy. Month is denoted by capital M.
Check below URL for valid formats
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Your formatter works quite fine (apart from the mm vs. MM bug). You get a formatted string from the date and then create a copy from your date by parsing the formatted string:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm");
Date now = Calendar.getInstance().getTime();
String formattedNow = sdf.format(now); // == "09/24/2013 01:59"
Date now2 = sdf.parse(formattedNow); // == now

java Date and time format

how to format "2011-10-25T13:00:00Z" string into date and time
i used simple date format class
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
but it only giving the date value. not time values
please help me to solve this problem
Use the format "yyyy-MM-dd'T'HH:mm:ss'Z'" for parsing this date format. See the documentation of SimpleDateFormat for more info. Code will look like this
String dateStr = "2011-09-19T15:57:11Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
Date date = new SimpleDateFormat(pattern).parse(dateStr);
This is because "yyyy-MM-dd" only mentions year (yyyy), month (MM) and date (dd). Try adding hh:mm if you want hours and minutes.
Example:
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd hh:mm");
System.out.println(sim.format(new Date())); // prints "2011-10-27 01:56"
The full documentation of the format-string and its parts is found here. The documentation includes this example:
"yyyy-MM-dd'T'HH:mm:ss.SSSZ" - 2001-07-04T12:08:56.235-0700
Perhaps it's something like that you're looking for.

Categories

Resources