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);
Related
Here's what I need to do :
I want to change the date2 into a Date but not String.
Date date = new Date();
DateFormat format = new SimpleDateFormat ("yyyy/MM/dd",Locale.ENGLISH);
String date2 = format.format(date);
It does not allow me to save date2 as a Date. Someone please help me. Thanks
java.util.Date objects do not have a format. They just represent a date value. You cannot "set the format of a Date object" because a Date object can't store this information.
What you are asking ("how do I set the format of a Date object") is not possible, because that's not how Date objects work.
What you should do instead is just work with the Date object and at the point where it needs to be displayed, you convert it to a String in the desired format using a SimpleDateFormat object.
import java.text.SimpleDateFormat;
import java.util.Date;
String your_date = "2014-12-15 00:00:00.0";
DateFormat format = new SimpleDateFormat ("yyyy/MM/dd",Locale.ENGLISH);
Date new_date = format.parse(your_date);
System.out.println(new_date);
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
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
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.
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));