Reputation: 68
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)
Upvotes: 1
Views: 923
Reputation: 43098
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.
Upvotes: 2
Reputation: 178263
Two problems:
Y
means something called a "week year", which is different than year. Use y
s instead.-
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");
Upvotes: 6