0

How to convert standard output of date into number of minutes?

I have output from my command as:

Mon Mar 4 12:33:58 2013

and I need to convert it into number of minutes say minutes1.

because I have a code which gives number of minutes for current time and the code is:

public class DateToMinutes {

   public static void main(String[] args) {

      Date date = new Date();

      long now = ((date.getTime()) / (60000));

      System.out.println("Total Minutes are  " + now);

   }

}

the output of this will be in minutes say minutes2.

I need to compare both minutes1 and minutes2.

since I am unable to convert Standard date into minutes1, it's not possible right now.

Adi
  • 5,089
  • 6
  • 33
  • 47
mac
  • 11
  • 1
  • 1
  • 3
  • look at parse method of SimpleDateFormat http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html – BigMike Mar 05 '13 at 10:56
  • (1) Time zone is crucial for the operation. Be specific about the time zone of your command output or else incorrect results are most likely. (2) The `Date` class was the right one to use in 2013. Today you should use [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) instead. (3) What is the point in obtaining the minutes? It seems simpler just to compare the `Date` objects with `before()` or `after()` (or better, their `java.time` counterparts with `isBefore()` or `isAfter()`). – Ole V.V. May 20 '18 at 10:47

3 Answers3

3

look at parse method of SimpleDateFormat, there you'll find formats used for converting dates.

Javadocs here

In your case something like this should work:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy");
Date theDate = sdf.parse ("Mon Mar 4 12:33:58 2013");
long minutes2 = theDate.getTime() / 60000;
BigMike
  • 6,683
  • 1
  • 23
  • 24
0

Use a simpledate formatter.

DateFormat formatter = new SimpleDateFormat("mm");
Date one = ...
Date two = ...
formatter.format( one) ); // only the minutes remain
formatter.format( two) ); // only the minutes remain

// do whatever you want.
duffy356
  • 3,678
  • 3
  • 32
  • 47
0

Determining minutes1 is parsing a date and transforming it.

Related issues can be found here and here.

This should do the job based on your description of the date format:

SimpleDateFormat yourStandardDateFormat = new SimpleDateFormat("EEE MMM dd kk:mm:ss yyyy");
Date date = yourStandardDateFormat.parse( stringRepresentingDate );
long minutes1 = date.getTime() / 60000l;

For reference, see SimpleDateFormat documentation

Community
  • 1
  • 1
cooltea
  • 1,113
  • 7
  • 16