Why is the month changed to 50 after I added 10 minutes?

The issue for you is that you are using mm. You should use MM. MM is for month and mm is for minutes. Try with yyyy-MM-dd HH:mm Other approach: It can be as simple as this (other option is to use joda-time) static final long ONE_MINUTE_IN_MILLIS=60000;//millisecs Calendar date = Calendar.getInstance(); long t= date.getTimeInMillis(); Date afterAddingTenMins=new … Read more

Get yesterday’s date using Date [duplicate]

Update There has been recent improvements in datetime API with JSR-310. Instant now = Instant.now(); Instant yesterday = now.minus(1, ChronoUnit.DAYS); System.out.println(now); System.out.println(yesterday); https://ideone.com/91M1eU Outdated answer You are subtracting the wrong number: Use Calendar instead: private Date yesterday() { final Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); return cal.getTime(); } Then, modify your method to the following: … Read more

Is there a date format to display the day of the week in java?

This should display ‘Tue’: new SimpleDateFormat(“EEE”).format(new Date()); This should display ‘Tuesday’: new SimpleDateFormat(“EEEE”).format(new Date()); This should display ‘T’: new SimpleDateFormat(“EEEEE”).format(new Date()); So your specific example would be: new SimpleDateFormat(“yyyy-MM-EEE”).format(new Date());

How do you format the day of the month to say “11th”, “21st” or “23rd” (ordinal indicator)?

// https://github.com/google/guava import static com.google.common.base.Preconditions.*; String getDayOfMonthSuffix(final int n) { checkArgument(n >= 1 && n <= 31, “illegal day of month: ” + n); if (n >= 11 && n <= 13) { return “th”; } switch (n % 10) { case 1: return “st”; case 2: return “nd”; case 3: return “rd”; default: return … Read more

How to parse a date? [duplicate]

You cannot expect to parse a date with a SimpleDateFormat that is set up with a different format. To parse your “Thu Jun 18 20:56:02 EDT 2009” date string you need a SimpleDateFormat like this (roughly): SimpleDateFormat parser=new SimpleDateFormat(“EEE MMM d HH:mm:ss zzz yyyy”); Use this to parse the string into a Date, and then … Read more