-2

I am developing an Android application in that I wants to get Millisecond from Date-Time.

I am using below code for the parsing but I can't get proper result.

public static long getTimeInMillisecond(String time) {
    // eg.  time = "27 Sep 2016 12:24PM";
    final SimpleDateFormat sdf = new SimpleDateFormat(
            "dd MMM yyyy hh:mma");

    sdf.setTimeZone(TimeZone.getDefault());

    try {
        Date mDate = sdf.parse(time);
        long timeInMilliseconds = mDate.getTime();
        return timeInMilliseconds;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}
Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
  • Possible duplicate of [Convert a date format in epoch](http://stackoverflow.com/questions/6687433/convert-a-date-format-in-epoch) – Basil Bourque Oct 06 '16 at 19:58

3 Answers3

0
 public static long getDateStringToLong(String str_date) {
        DateFormat formatter;
        Date date = null;
        formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.US);
        try {
            date = (Date) formatter.parse(str_date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

change date format as per your requirement Hope this helps..

Nikhil Borad
  • 2,065
  • 16
  • 20
0

Hello you can try to this code..

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");    
 String currentDateandTime = sdf.format(new Date());        
 Log.e("Log","Time----"+currentDateandTime);    
Dileep Patel
  • 1,988
  • 2
  • 12
  • 26
0

Finally got the solution:

Adding Locale.English with SimpleDateFormat and working fine.

final SimpleDateFormat sdf = new SimpleDateFormat(
            "dd MMM yyyy hh:mma", Locale.ENGLISH);
Sagar Zala
  • 4,854
  • 9
  • 34
  • 62