I am reading date values from a file in which i am reading it as string object.
The date values varies in many format for example sew below:
01-Mar-2012
01/12/2012
01-12-2012
01.12.2012
01.Mar.2012
07/01/2008 12:00:00
07/01/2008 12:00:00 AM
What ever be the format i want the date object in format dd/mm/yyyy.
There is so many combinations.
SimpleDateFormat expect me to provide the pattern to format it .
Is there is any way to guess the pattern or create a date object from the given string?
how can you know if 12-6-2012 is 6th december or 12th june?!
what you could do is defining an array with all possible patterns.
then try to parse the date for each array entry (if it throws an exceptionen try the nexct pattern and so on)
i know that this is a pretty ugly attempt but it works!
What ever be the format i want the date object in format dd/mm/yyyy.
A java.util.Date object does not have a format - the only information it contains is an instant in time (specifically, the number of milliseconds since 01-01-1970, 00:00:00 GMT).
So, you cannot have a "date object in format dd/mm/yyyy". Date objects don't have a format, just like numbers don't have an inherent format.
If you want to display the date in the format dd/mm/yyyy, then you have to convert it to a string first using a SimpleDateFormat object; you specify the format on the SimpleDateFormat object (not on the Date object itself).
// NOTE: use MM instead of mm; MM = months, mm = minutes
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date now = new Date();
// Display the date in the format dd/MM/yyyy
System.out.println(df.format(now));
Related
This question already has answers here:
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 8 years ago.
I want to display current date in MM-dd-yy format
Date d1=new Date();
Now I want to change in to the mm-dd-yy pattern
So I formatted it using simple date format
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(MM-dd-yyyy);
Pattern
Now d1 is in string type but I want it as Date type
This is the difference between model and view.
The model here is the Date object (Date date = new Date();) It contains information about what month, day, and year, hour of day, etc. that the date was created with. It doesn't know anything about formatting.
The view is the string generated by the DateFormat object. You can pass the model (the date object) in to the formatter in order to generate multiple views of the model depending on what fields you're interested in, and how you want them presented.
So the date continues to hold the same date fields regardless of what strings the formatter generates for it. The date and the formatted strings are separate objects. Although we say "format a date", the formatting doesn't actually change the date object in any way.
Your format string is incorrect, to get the month you want "MM-dd-yyyy", "mm" means minutes. See the API documentation for SimpleDateFormat to see what the different pattern letters mean.
I am storing datetime as text in the format dd-MM-yyyy hh:mm:ss in SQLite database. I want to convert it to a date object in the same format. I'm only able to convert it to the format Mon Jun 16 01:35:38 GMT+05:30. How can I have it as a Date object of the required format? Here TSArray[] contains the dates in string format:
int dateArraySize=TSArray.length;
Date dateArray[]=new Date[TSArray.length];
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
for (int i=0;i<TSArray.length;i++) {
try {
//converting String date to Date
Date result=df.parse(TSArray[i]);
dateArray[i]= result;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date objects in Java simply represent an instant in time (specifically, the number of milliseconds before or after the epoch) - they don't store any information about how they should be displayed, nor should they. Formatting a date is purely for display purposes, and is the concern of the DateFormat hierarchy of classes and shouldn't be done until display time (unless you have a compelling reason to do it earlier than that, but I can't think of a good one).
There's arguably nothing to stop you sending a DateFormat object alongside your Date, but you should never need to if you properly internationalise your code in the first place.
One reason that you shouldn't do this is down to the difference in regional date formats. If you presented me with the date 03/07/2014, I'd see that as the 3rd of July 2014, because here in the UK we format our dates as DD/MM/YYYY. Present the same date to someone in the USA and they'll see it as March the 7th 2014, because US dates are typically MM/DD/YYYY.
The database and the server don't need to know any locale information about the client, they should simply serve the requests for data where appropriate and let the client (which does know the locale information) format that data appropriately.
TL;DR:
You can't, because Java Date objects don't store display formatting rules.
As you're using AChartEngine you should be able to achieve what you're after by using a TimeChart and passing in the date format mask via a call to the chart's setDateFormat(String) method.
Database return value in java.sql.date n java default you are using java.util.date. Java.util.date convert into java.sql.Date like below given..
Calendar cal = Calendar.getInstance();
Java.sql.Date sqlDate = new java.sql.Date(cal.getTimeInMillis());
I'm trying to format a Date object and convert that formatted one back to Date type object
This is my code
SimpleDateFormat inputDf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zzz");
System.out.println("before format "+invoiceDate);
invoiceDate=inputDf.parse(inputDf.format(invoiceDate));
System.out.println("after format "+inputDf.format(invoiceDate));
System.out.println("after parse "+invoiceDate);
Out put from above code is
before format : Mon Jan 14 10:55:40 IST 2013
after format : Mon Jan 14 2013 10:55:40 IST
after parse : Mon Jan 14 10:55:40 IST 2013
You can see here after i parse the date object it converting back to original format(format which shows before format) but i want Date object like it appear in second print line(after format) thing is .format method returns String not a Date object, how can i fix this ?
Thanks
The Date object doesn't define any format. Date is just a long number with time in it.
In order to modify its format you have to use a String object, as you did in your example.
If you analyze your example:
before format: Shows the DEFAULT format for a Date that System.out.println offers.
after format: Shows the format given after using the SimpleDateFormat.
after parse: We are again in the first case.
This behaviour is because the SimpleDateFomat does not impose itself upon the date. The SimpleDateFormat is merely a pattern to get a formated output out of any Date, but it does not change the date itself. Date does not have a format, so
System.out.println("before format "+invoiceDate);
defaults to the default pattern format.
Actually, the SimpleDateFormat is exactly the way to achieve what you want, ie use it to properly format your output everytime you need it. Formating a date object gives you a representation of it, a String.
System.out.println("after parse "+invoiceDate);
Here you just trying to print the Date object . The Date object , per se, doesn't have any format. The class Date represents a specific instant in time, with millisecond precision. Hence we use DateFormat to format the output string which is printed on printing the Date object . To display the same output as second statement , you need to format it again.
System.out.println("after parse"+inputDf.format(invoiceDate));
Look at the Javadoc for implementation of the toString() for Date :
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
How can I get the current date of the system with this format yyyy-MM-dd
I want this
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String strDate = formatDate.format(now);
return strDate;
but returning a Date instead of a String.
Any help is greatly appreciated.
UPDATE: If that's the case, can I parse my String to Date?
How can i get the current date of the system with this format yyyy-MM-dd but returns Date instead of String.
You can't. There's no such thing as "a Date with a format" any more than there's the concept of "an int with a format". A Date value is just a point in time, with no associated text format, calendar system or time zone.
Using new Date() will get you a Date object representing the current instant in time, and nothing else. How you use that is up to you - but if you return it from a method then there is no associated date (as the date will vary by time zone), no format etc - it's up to the calling code to use it appropriately.
You might want to consider using Joda Time which at least has a LocalDate type - although you still need to consider which time zone you want to use when you think about "the current date". (And there's still no formatting information associated with the value.)
EDIT: To answer your update, you can just use SimpleDateFormat to parse - but it's not clear where your string has come from to start with. This sounds like the opposite requirement from the rest of your question.
since you cant change Date format build your own CustomDate, it is just a representation of time.
on the method which recieves the date as a string
use another simpledateformatter
and convert the string into date by using
simpledateformatter.parse(strDate);
You can use this .!!
String formatDate = new SimpleDateFormat("yyyy-MM-dd").format( yourDate);
I've got the following code:
Date time = new SimpleDateFormat("HH:mm").parse("8:00");
When I call time.toString(), the following is produced:
Thu Jan 01 08:00:00 CET 1970
Is there any way I can extract just the 8:00 from it? I have searched far and wide and have not found any way to do it using the standard SimpleDateFormat.
When I call time.toString(), the following is produced
Yes, it would be - because you're calling Date.toString. A Date value has no concept of format.
Is there any way I can extract just the 8:00 from it?
Whenever you want to convert to a string, you should use a DateFormat. So use the same format that you parsed in.
Alternatively, use Joda-Time, which has a LocalTime type specifically for "time of day", and has a handy parse method. You should still use a formatter every time you want to convert to a string, but at least the value will be easier to work with and more descriptive before then.
LocalTime localTime = LocalTime.parse("8:00");
To format this, you can use something like ISODateTimeFormat.hourMinute() or if you might have more precision, perhaps ISODateTimeFormat.hourMinuteSecond() - see the docs for all of the many options available.
recycle your original SimpleDateFormat Object
SimpleDateFormat format = new SimpleDateFormat("HH:mm")
Date time = format.parse("8:00");
String outString = format.format(time);
in case you were wondering, Here's some more information on DateTime Masks
Use the same SimpleDateFormat instance to format date into string.
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date time = sdf.parse("8:00");
System.out.println(sdf.format(time));
This will print:
08:00
java.util.Date class represents a specific instant in time, with millisecond precision.
API says java.util.Date.toString()
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
In order to format date's use SimpleDateFormat class
System.out.println(new SimpleDateFormat("HH:mm").format(time));