1

I want an output in Month,Year wise only but I'm getting a problem. It is an Exception of Unparseable date: "Apr 5, 2018". I have stored the date in Bill_Date Column in sqlite and it has its format of "Apr 5, 2018" in its field. So how can I get only Month and year in that format (Apr, 2018) only. This is my code given below:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class Demo2 {
    private static Connection con;
    private static String query;
    private static PreparedStatement PStat,PStat2;
    private static ResultSet res,res2;
    private static String Bill_Date;
public static void main(String args[])
{
    try
    {
        con=Database.Database();
        query="select Bill_Date from DailySales where Bill_Date='Apr 5, 2018'";
        PStat=con.prepareStatement(query);
        res=PStat.executeQuery();
        while(res.next())
        {
        Bill_Date=res.getString("Bill_Date");
        }
        SimpleDateFormat month=new SimpleDateFormat("MMMM,yyyy",Locale.ENGLISH);
        SimpleDateFormat dateformat=new SimpleDateFormat("MMM-dd,YYYY");
        Date date=dateformat.parse(Bill_Date);
        String MONTH=month.format(date);
        System.out.println(MONTH);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
}

It has this error.

java.text.ParseException: Unparseable date: "Apr 5, 2018"
    at java.text.DateFormat.parse(Unknown Source)
    at Demo2.main(Demo2.java:34)
rgettman
  • 176,041
  • 30
  • 275
  • 357
md azaz
  • 68
  • 7
  • 7
    Ask yourself the question - how is `MMM-dd,YYYY` the same as `Apr 5, 2018`? – MadProgrammer Apr 12 '18 at 22:50
  • Unless you have complete control over the input strings, your enterprise is doomed to fail. Date formats produced by random external programs vary too much for Java's dateformat classes to handle. – ddyer Apr 13 '18 at 03:12
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 14 '18 at 03:10

2 Answers2

6

Two problems:

  1. Using capital Y means something called a "week year", which is different than year. Use ys instead.
  2. You have a - in the format that doesn't exist in the date string. Make the format have a space (or make the data have a -).

Use this:

SimpleDateFormat dateformat = new SimpleDateFormat("MMM d,yyyy");
rgettman
  • 176,041
  • 30
  • 275
  • 357
2

Missing a space in the formatter and you also have an extra -:

SimpleDateFormat dateformat=new SimpleDateFormat("MMM dd, yyyy");

Thanks @David

Also it seems your year format is wrong. Use yyyy for the correct year format.

smac89
  • 39,374
  • 15
  • 132
  • 179