How to convert a string Date to long millseconds

前端 未结 9 2102
你的背包
你的背包 2020-11-29 07:40

I have a date inside a string, something like \"12-December-2012\". How can I convert this into milliseconds (long)?

相关标签:
9条回答
  • 2020-11-29 08:16

    you can use the simpleDateFormat to parse the string date.

    0 讨论(0)
  • 2020-11-29 08:18

    Take a look to SimpleDateFormat class that can parse a String and return a Date and the getTime method of Date class.

    0 讨论(0)
  • 2020-11-29 08:21

    Easiest way is used the Date Using Date() and getTime()

        Date dte=new Date();
        long milliSeconds = dte.getTime();
        String strLong = Long.toString(milliSeconds);
        System.out.println(milliSeconds)
    
    0 讨论(0)
  • 2020-11-29 08:26
    • First convert string to java.util.Date using date formatter
    • Use getTime() to obtain count of millisecs from date
    0 讨论(0)
  • 2020-11-29 08:33
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
    Date date = (Date)formatter.parse("12-December-2012");
    long mills = date.getTime();
    
    0 讨论(0)
  • 2020-11-29 08:38

    Using SimpleDateFormat

    String string_date = "12-December-2012";
    
    SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
    try {
        Date d = f.parse(string_date);
        long milliseconds = d.getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    
    0 讨论(0)
提交回复
热议问题