Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Playing with date format in Android

    • 0
    • 3
    • 2
    • 2
    • 0
    • 0
    • 0
    • 0
    • 640
    Comment on it

    We can change date format of a date as below:

    Here, I have defined different date formats:

    String FACEBOOK_DATE_FROMAT = "yyyy-MM-dd'T'HH:mm:ssZZZZZ";
    String MESSAGE_DATE_FORMAT = "MM/dd/yyyy hh:mm a";
    String MESSAGE_DATE_TIME_FORMAT = "MM/dd/yyyy HH:mm:ss";
    String TIME_FORMAT = "hh:mm a";
    String DAY_TIME_FORMAT = "E hh:mm a";
    String MONTH_DAY_FORMAT = "MMMMMMMMM dd";
    String MONTH_YEAR_FORMAT = "MMMMMMMMM dd yyyy";
    

    Convert long date into proper date fromat :

    long longDate = 1384099496;  
    String dateString = new SimpleDateFormat(MESSAGE_DATE_TIME_FORMAT,Locale.getDefault()).format(new Date(longDate));
    

    Output:
    date will be in the format of MM/dd/yyyy HH:mm:ss.

    Convert Facebook datetime format:

    Suppose we have facebook date as: 2013-11-30T07:41:39+0000 and we want to convert this into 11/30/2013 7:41, then we will do it as below:

    SimpleDateFormat simple=new SimpleDateFormat(FACEBOOK_DATE_FROMAT, Locale.getDefault());
        Date dateStr = null;
    try {
        dateStr = simple.parse(modifiedDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    String date = new SimpleDateFormat(Constant.MESSAGE_DATE_FORMAT,Locale.getDefault()).format(dateStr);
    

    Output:
    Output will be 11/30/2013 7:41 AM

    Convert date format to fullMonth date year:

    String date = new SimpleDateFormat(MONTH_YEAR_FORMAT, Locale.getDefault()).format(dateStr);
    

    Output:
    Output will be November 30 2013

    Convert date format to fullMonth date:

    String date = new SimpleDateFormat(MONTH_DAY_FORMAT, Locale.getDefault()).format(dateStr);
    

    Output:
    Output will be November 30

    Convert date format to weekday date hour:minutes AM_PM:

    String date = new SimpleDateFormat(DAY_TIME_FORMAT, Locale.getDefault()).format(dateStr);
    

    Output:
    Output will be Saturday 30 7:41 AM

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: