Reputation: 23035
Plase have a look at the below code
Calendar date = Calendar.getInstance();
initialClientLetterDate.setText(date.get(Calendar.YEAR)+"/"+date.get(Calendar.MONTH)+"/"+date.get(Calendar.DAY_OF_WEEK));
This generates the invalid "month" and "date". The output is 2014/09/06
. Why is this? I just wanted to get current year, date and month.
Upvotes: 0
Views: 600
Reputation: 340118
java.time.LocalDate
In modern Java, use the LocalDate
class to represent a date.
The toString
method generates text in standard ISO 8601 format YYYY-MM-DD. To get your desired output, swap the hyphen for a slash.
LocalDate ld = LocalDate.of ( 2025, Month.JANUARY , 23 ) ;
String output = ld.toString().replace ( "-" , "/" ) ;
Upvotes: 1
Reputation: 3162
You can do like as below code. You should not forgot to set locale.
public static void main(String args[]) {
Calendar date = Calendar.getInstance(Locale.US);
System.out.println(date.get(Calendar.YEAR) + "/"
+ (date.get(Calendar.MONTH) + 1) + "/"
+ date.get(Calendar.DAY_OF_MONTH));
}
Upvotes: 2
Reputation: 2546
date.get(Calendar.YEAR) = Gives the current year date.get(Calendar.MONTH) = Gives the month of the year as an integer from 0 to 11, where 0 = Jan and 11 = dec date.get(Calendar.DAY_OF_WEEK) = Gives the day of the week from 0 to 6, where 0 = Monday
So if you want to get the current date you must do
date.get(Calendar.YEAR)+"/"+(date.get(Calendar.MONTH)+1)+"/"+date.get(Calendar.DAY_OF_MONTH)
Upvotes: 0
Reputation: 1
DAY_OF_WEEK Returns an int (starting at 1?) for the day in the week so Friday would be 6, Month does the same except starting at 0 so 09 would be October.
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#DAY_OF_WEEK
Upvotes: 0
Reputation: 23012
You are using DAY_OF_WEEK
which is 6 for FRIDAY
and MONTH
starts from 0 not 1 so you have to add 1 in it.You can use DAY_OF_MONTH
instead of DAY_OF_WEEK
.
Upvotes: 5