Thursday 8 August 2013

Convert date from date to any date

Convert date from date to any date using below function

public static String convertDate(String date, String fromFormat,
            String toFormat)
    {
        String formattedDate = "";
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fromFormat);
            Date d = simpleDateFormat.parse(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(d);
            simpleDateFormat = new SimpleDateFormat(toFormat);
            simpleDateFormat.setCalendar(calendar);
            formattedDate = simpleDateFormat.format(calendar.getTime());

        } catch (Exception e) {
            if (e != null)
                e.printStackTrace();
        }
        return formattedDate;
    }


now you can use this function like below

//for example :-mydate = 12/01/2000

String mydate =  convertDate("mydate", "MM/dd/yyyy", "dd MMMM yyyy");

No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...