Reputation:
I have a problem in date.
For example now is February 4, 2016.
And I am populating(I don't know the exact rem) the textview with the current date(dd) but the result I am getting is February 35 2016.
this is my code:
DateFormat dateFormat1 = new SimpleDateFormat("D");
String cDay = dateFormat1.format(new Date());
Day.setText(cDay);
Upvotes: 0
Views: 2243
Reputation: 1981
Try this:-
DateTimeFormat.forPattern("d").print(DateTime.now());
It returns string value
Upvotes: 0
Reputation: 381
Try below code
DateFormat dateFormat1 = new SimpleDateFormat("d MMM yyyy");
String cDay = dateFormat1.format(new Date());
Day.setText(cDay);
hope this code will help you..
Upvotes: 0
Reputation: 2737
D - Day in year
and d - Day in month
u can use below-
DateFormat dateFormat1 = new SimpleDateFormat("dd");
String cDay = dateFormat1.format(new Date());
Day.setText(cDay);
Upvotes: 1
Reputation: 22965
Try this code,
public static String getDateFormat(String date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String convertedDate = "";
try {
Date parseDate = dateFormat.parse(date);
SimpleDateFormat fmtOut = new SimpleDateFormat("yyyy-MM-dd");
convertedDate = fmtOut.format(parseDate);
} catch (ParseException e) {
e.printStackTrace();
}
return convertedDate;
}
OR
public static String getTodaysDate() {
Calendar currentDate = Calendar.getInstance(); //Get the current date
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy "); //format it as per your requirement
return formatter.format(currentDate.getTime());
}
Upvotes: 0