user3660378
user3660378

Reputation:

Android : Simple Date Format

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

Answers (5)

Sabari
Sabari

Reputation: 1981

Try this:-

DateTimeFormat.forPattern("d").print(DateTime.now());

It returns string value

Upvotes: 0

Amey Bawiskar
Amey Bawiskar

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

kevz
kevz

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

Reaz Murshed
Reaz Murshed

Reputation: 24241

new SimpleDateFormat("EEE, MMM d, ''yy, H:MM a")

Upvotes: 0

Amit Vaghela
Amit Vaghela

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

Related Questions