I'm trying to format a date using the method where dd-mm-YYYY is an example date
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(displayDate*1000);
String date = DateFormat.format("dd-mm-YYYY", cal).toString();
I'm looking for a list of characters I can use, similar to this in PHP, to format my date how I want. Is there a list anywhere around I can use? The format I'm after is 26th April, 2006, but a list would be good so I could bookmark it for reference later on.
The JavaDoc of SimpleDateFormat does contain such a list.
Yes, it's in the javadoc for SimpleDateFormat:
http://docs.oracle.com/javase/7/docs/api/
Related
I am doing up a script where by the user will pass in string date (MM/DD/YYYY). I want to compare this string date to the "today" date to find out the days different between these 2 days.
This is my codes to get "today" date.
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
returnDate = dateFormat.format(cal.getTime());
How do i go about using date to minus date? convert them to timestamp and convert it back?
Thank you
getTimeInMillis() and the relevant division will tell you how many days different. Not sure why converting it back is helpful.
If you can use JodaTime library. A similar question with usage of this library was already answered here.
In this first i get the current date and then minus by 2 then using formatter and set the same accordingly. So the ouput you will get will be 2 days before todays date, Please try below:-
Calendar cal=Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -2);
Date dt=cal.getTime();
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
String ss=format.format(dt);
System.out.println(ss);
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);
Hi I want to iterate through a date range without using any libraries. I want to start on 18/01/2005(want to format it to yyyy/M/d) and iterate in day intervals until the current date. I have formatted the start date, but I dont know how I can add it to a calendar object and iterate. I was wondering if anyone can help. Thanks
String newstr = "2005/01/18";
SimpleDateFormat format1 = new SimpleDateFormat("yyyy/M/d");
Date date = format1.parse(newstr);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
while (someCondition(calendar)) {
doSomethingWithTheCalendar(calendar);
calendar.add(Calendar.DATE, 1);
}
Use SimpleDateFormat to parse a string into a Date object or format a Date object into a string.
Use class Calendar for date arithmetic. It has an add method to advance the calendar, for example with a day.
See the API documentation of the classes mentioned above.
Alternatively, use the Joda Time library, which makes these things easier. (The Date and Calendar classes in the standard Java API have a number of design issues and are not as powerful as Joda Time).
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));
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
format date from 14 aug to YYYYMMDD
Hi I need to get a date in a YYYYMMDD format. I Don't know the right method for that...
I set the date with this lines of code...
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DATE, date);
Later in the code I need to extract the YYYYMMDD date value, how I can do that?
Thanks
You would use SimpleDateFormat:
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
format.setCalendar(cal);
String text = format.format(cal.getTime());
Personally I would strongly recommend that you look at using Joda Time instead though - it's a much nicer API than the built-in Date/Calendar class.
You can use the SimpleDateFormat class.
SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMDD");
String date = sdf.format(new Date());
The above code should give you the current date in the format YYYYMMDD.
I think you should look to the API documentation for SimpleDateFormat class. It provides a format method.
SimpleDateFormat
It's great for problems like this
According to the Calendar documentation, you should use the DateFormat class. The documentation for DateFormat can be found here
If these do not suit your needs, then you can make a custom wrapper class for Calendar and override the toString() method