-2

How can I do that? I tried a lot but it is not working properly.

public static String getDateTime() {
        String dateStr = "";
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateStr = dateFormat.format(date);
        return dateStr;
    }

Please someone right another method which return miliseconds from above Date format. Of course! In my DB I'm storing this value: 2019-04-17 17:11:02 -> I want this value to convert in miliseconds using one method. so that I call new method and pass that variable value.

Kiryl Tkach
  • 3,118
  • 5
  • 20
  • 36
  • you only want to convert time or time +date? – A Farmanbar Apr 13 '19 at 11:51
  • do you have date in string above format when you get from database and want to covert it back to Date object and milliseconds? is this what you want? – Asad Ali Choudhry Apr 13 '19 at 11:52
  • when I get back that String value from DB, I want that value to convert and stroe in long variable as a miliseconds – Pooja Singh Apr 13 '19 at 12:16
  • @AsadChoudhary chk – Pooja Singh Apr 13 '19 at 12:17
  • @Mr.AF when I get back that String value from DB, I want that value to convert and stroe in long variable as a miliseconds – – Pooja Singh Apr 13 '19 at 12:17
  • @luk2302 when I get back that String value from DB, I want that value to convert and stroe in long variable as a miliseconds – – Pooja Singh Apr 13 '19 at 12:17
  • Look at my answer, its returning you milliseconds in long variable. – Asad Ali Choudhry Apr 13 '19 at 12:31
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Apr 13 '19 at 12:48

3 Answers3

1

try this:-

       long timeInMilliseconds = 0;
        String givenDateString = "2019-04-17 17:11:02";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date mDate = sdf.parse(givenDateString);
            timeInMilliseconds = mDate.getTime();
        } catch (ParseException e) {
                    e.printStackTrace();
        }

String timeInMilliseconds = String.valueOf(timeInMilliseconds);
Sandeep Malik
  • 1,972
  • 1
  • 8
  • 17
1
 public static long milisecondsFromDate(String dateStr) {

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        Date date = formatter.parse(dateStr);
        return date.getTime();
    } catch (Exception e) {
        Log.e("Tag", "Wrong date Format");
    }
    return -1;
}
Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
1

I would strongly suggest to use Java8 Date/Time API for your cause, something like:

public static void main(String[] args) {
    System.out.println(getDateTime());
}

public static String getDateTime() {
    String dateStr = "2019-04-17 17:11:02";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
    LocalDateTime localDateTime = LocalDateTime.parse(dateStr, formatter);

    return String.valueOf(localDateTime.toInstant(ZoneOffset.UTC).getEpochSecond());
}
MS90
  • 1,219
  • 1
  • 8
  • 17