String date="21-04-2013";
In my android application
Here i want to display the date in the following format like "21" is a separate string and month is like "Apr" as a separate string and year is like "13"as a separate string without using String functions.Can anybody plz give some suggestions to convert in this format?any date function is available?
You'll want to take a look at the SimpleDateFormat class for parsing the date string. In order to end up separate strings without using string functions, you'll probably need multiple formatters for the output too. It would look somewhat like this:
String date = "21-04-2013";
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy"); // input date
Date outDate = dateFormatter.parse(date);
SimpleDateFormat dayFormatter = new SimpleDateFormat("dd"); // output day
SimpleDateFormat monthFormatter = new SimpleDateFormat("MMM"); // output month
SimpleDateFormat yearFormatter = new SimpleDateFormat("yy"); // output year
String day = dayFormatter.format(outDate);
String monthy = monthFormatter.format(outDate);
String year = yearFormatter.format(outDate);
If you were to use String.split(), you could get rid of at least two of the formatters in above snippet.
This is the class you'll need: DateFormat
Examples are provided in the link, but in short, you'll first need to parse the date, and then format the date again.
Related
I have a String reading clients date of birth and I am trying to assign to another String but I need to assign in yyyymmdd format. I am not sure what format client set his birth date.
Here is my code.
String birthDate = request.getClient().getBirthDate();
patient().setDob_yyyymmdd(birthdate);
Do you have to create a SimpleDateFormat:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//Put the incoming format
Then create a
Date date =sdf.parse(yourincomingdate);
Then create a DateFormat to transfor your Date to another format
DateFormat df = new SimpleDateFormat("HH:mm");
String reportDate = df.format(date);
There is no way you can guess in which format is the input date.
However, if you can manage to discover which is the input format, you can use the SimpleDateFormat class to convert Date objects to Strings and vice-versa.
String inputDate, inputFormat; //inputFormat may be, eg, "MM-dd-yyyy", that is the part you still need to find out
Date foo = new SimpleDateFormat(inputFormat).parse(inputDate);
String output = new SimpleDateFormat("ddMMyyyy").format(foo);
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
This question already has answers here:
Parse String to Date with Different Format in Java
(10 answers)
Closed 1 year ago.
I have a date and time picker and I build a string date using these two. Here's a sample date and time string.
"11/6/2013 09:23"
Now I need to convert them into a date and convert them to this format "yyyy-MM-dd'T'HH:mm:ss.SSS".
My problem is I'm having this error in my logcat:
11-06 21:23:53.060: E/Error(26255): Unparseable date: "11/6/2013 09:23" (at offset 2)
I'm using this code to do the conversion of string to date, and the date to a formatted string.
Date d = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.getDefault()).parse(etaToDeliverArrivedAtShipper.getText().toString());
ETAtoNextStop = d.toString();
When I use new Date and get the current date, it works fine. I guess the format of my string is wrong. But I'm displaying it on an edittext in that format. I want to stay it in that way. Is there anyway to convert this string format to a date? Any ideas guys? Thanks!
I think you are trying to do 2 things at once. In order to convert any String to another String in a different format you need to:
Parse the string with a DateFormat containing the current format, this returns a Date; then
take your newly obtained Date and format it under the desired DateFormat
SimpleDateFormat formatOne = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date date = formatOne.parse(etaToDeliverArrivedAtShipper.getText().toString());
SimpleDateFormat formatTwo = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS",Locale.getDefault());
String result = formatTwo.format(date)
you are trying to parse in yyyy-MM-dd'T'HH:mm:ss.SSS but it is not. it is probably yyyy/M/dd HH:mm:ss
What you probably want is:
Date d = new SimpleDateFormat("yyyy/M/dd HH:mm:ss", Locale.getDefault()).parse(etaToDeliverArrivedAtShipper.getText().toString());
ETAtoNextStop = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.getDefault()).format(d);
This is simply because your pattern is not matching the date string you are trying to parse. There are 3 errors in your pattern :
You use MM which means the month should be on two digits, while in the date it is only 1 digit
You use .SSS which means there are milliseconds but your date is not that precise
You are using the wrong delimiter
So the right pattern should be : yyyy/M/dd HH:mm:ss
To get the desired format, then create a new SimpleDateFormat object with the desired pattern and use the parse(Date) method giving it the Date object previously returned by parse().
I have read a lot of questions and searched for a lot of libs all over the internet but I can't find one that can do this quickly.
I want to parse a specific date in a specific date format like this:
String date = "20130516T090000";
SimpleDateFormat x = new SimpleDateFormat("yyyyMMddTHHmmss");
String theMonth = x.parse(date, "M"); // 05
String theMonth = x.parse(date, "MMM"); // MAY
String theMinute = x.parse(date, "mm"); // 00
String theYear = x.parse(date, "yyyy"); // 2013
Just simple as that. A way to set a Parse Rule, a Specific Date Format and a way to retrieve each data i want (Month, Minute, Year....)
Is there a good library to do EXACTLY this? If yes could you put an example together? If no, is there a good way to do this without much code?
Thanks in advance!
Use the SimpleDateFormat class to parse a date from a String to a Date instance.
String date = "20130516T090000";
SimpleDateFormat x = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
Date d = x.parse(date);
There was a problem in your SimpleDateFormat format String, text in the format has to be quoted using single quoted (') to avoid interpretation.
Use the Calendar class to get the part of the date you want.
Calendar cal = Calendar.getInstance();
cal.setTime(d);
String theYear = String.valueOf(cal.get(Calendar.YEAR));
String theMonth = String.valueOf(cal.get(Calendar.MONTH));
String theMinute = String.valueOf(cal.get(Calendar.MINUTE));
I have this code block where argument to dateFormat.format will always be a string thats why I did .toString() here. I am getting error "Cannot format given Object as a Date".
Is there any way to do this ? Note that string is coming from database I used new Date() as a sample here.
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMMM dd, yyyy");
String sCertDate = dateFormat.format(new Date().toString());
DateFormat#format accepts a Date, not a string.
Use
String sCertDate = dateFormat.format(new Date());
If you have a string coming from the database that is a specific format and you want to convert into a date, you should use the parse method.
#Sonesh - Let us assume you have a string in the database that happens to represent a Date ( might be better to store the object in the database as dates? ) , then you would first parse it to the format you wanted and then format it to the string format you wanted.
// Assumes your date is stored in db with format 08/01/2011
SimpleDateFormat dateFormatOfStringInDB = new SimpleDateFormat("MM/dd/yyyy");
Date d1 = dateFormatOfStringInDB.parse(yourDBString);
SimpleDateFormat dateFormatYouWant = new SimpleDateFormat("MMMMM dd, yyyy");
String sCertDate = dateFormatYouWant.format(d1);
There are two applications of SimpleDateFormat:
parse a string - when you have a date represented as string, and you want to get the corresponding Date object. Then use dateFormat.parse(string)
format a date - when you have a Date object and you want to format it in a specific way (usually in order to show it to a user). In that case use dateFormat.format(date)
The two methods are reciprocal - one takes a date and returns a string, and the other takes a string and returns a date.
For your particular case, perhaps you need .parse(..). But note that every 'self-respecting' database driver should have an option to return a Date rather than some string representation. If you happen to be storing dates as string in the DB - don't do that. Use the native date type.
If you need to read a Date with one String format and output it to another String format, you need 2 formatters, for example:
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat outputFormat = new SimpleDateFormat("MMMMM dd, yyyy");
String output = outputFormat.format(inputFormat.parse(input));