I am working on Google charts API and
Google Visualization Candlestick Charts expects data to be an array of arrays for it to work .
For example the below format
[
[ "2011-08-01", 136.65, 136.96, 134.15, 136.49 ],
[ "2011-08-02", 135.26, 135.95, 131.50, 131.85 ],
[ "2011-08-05", 132.90, 135.27, 128.30, 135.25 ]
]
This is my SQL by which i am retrieving the data from database
String selectsql = "select current_day , open_val , high_val , low_val , close_val from historical_data where symbol_name = ?";
while(Rset.next())
{
String current_day = SgxRset.getString("current_day");
String open_val = SgxRset.getString("open_val");
String high_val = SgxRset.getString("high_val");
String low_val = SgxRset.getString("low_val");
String close_val = SgxRset.getString("close_val");
}
Could you please tell me how to construct how to construct an array of arrays ??
sample program
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
public class TestJSON {
public static void main(String[] args) throws JSONException {
Employee emp1 = new Employee();
emp1.setName("Ravi");
emp1.setName("20");
Employee emp2 = new Employee();
emp2.setName("Kiran");
emp2.setName("20");
List<Employee> histList = new ArrayList<Employee>();
Object[] arrayResultData = histList.toArray();
}
}
public class Employee {
String name ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
String age ;
}
Create a class which will held all these data together in an object like this with getters and setters:
public class Data {
private String current_day;
private double open_val;
private double high_val;
private double low_val;
private double close_val;
public Data(String current_day, double open_val, double high_val,
double low_val, double close_val) {
this.current_day = current_day;
this.open_val = open_val;
this.high_val = high_val;
this.low_val = low_val;
this.close_val = close_val;
}
public String getCurrent_day() {
return current_day;
}
public void setCurrent_day(String current_day) {
this.current_day = current_day;
}
public double getOpen_val() {
return open_val;
}
public void setOpen_val(double open_val) {
this.open_val = open_val;
}
public double getHigh_val() {
return high_val;
}
public void setHigh_val(double high_val) {
this.high_val = high_val;
}
public double getLow_day() {
return low_val;
}
public void setLow_day(double low_day) {
this.low_val = low_day;
}
public double getClose_day() {
return close_val;
}
public void setClose_day(double close_day) {
this.close_val = close_day;
}
}
Now, store the data in array of objects which you are getting from SQL.
String selectsql = "select current_day , open_val , high_val , low_val ,close_val from historical_data where symbol_name = ?";
List<Data> myList = new ArrayList(Data);
while(Rset.next())
{
String current_day = SgxRset.getString("current_day");
String open_val = SgxRset.getString("open_val");
String high_val = SgxRset.getString("high_val");
String low_val = SgxRset.getString("low_val");
String close_val = SgxRset.getString("close_val");
Data data = new Data(current_day,open_val,high_val,low_val,close_val);
myList.add(data);
}
For reading data, you can write:
String a = "[ ";
for (int i = 0; i < myList.size(); i++) {
String b = "[ \"" + myList.get(i).getCurrent_day() + "\" , "
+ myList.get(i).getOpen_val() + "],";
if(i==myList.size() -1)
a += b;
else
a += b+",";
}
a += " ]";
Now, for me the output is:
[ [ "2011-08-01" , 136.65],[ "2011-08-02" , 135.26] ]
I recommend a List. You don't know what your columns are and how many you have:
List<HistData> histList = new ArrayList<HistData>();
//.. query db
while(Rset.next()){
HistData histData = new HistData();
histData.setCurrentDay( SgxRset.getString("current_day"));
histData.setOpenVal( SgxRset.getString("open_val"));
histData.setHighVal( SgxRset.getString("high_val"));
histData.setLowVal( SgxRset.getString("low_val"));
histData.setCloseVal( SgxRset.getString("close_val"));
histList.add(histData);
}
HistData is just a POJO, but you can change the datatypes in the bean and the SgxRset retrieval type, so you are not locked in to any array type. Using a List as opposed to an array provides more flexibility in case you are returning many results of unknown length.
public class HistData {
public HistData(){}
String currentDay;
public setCurrentDay(String currentDay){
this.currentDay = currentDay;
}
public getCurrentDay(){
return currentDay;
}
// .. etc.
}
Use a List and the toArray Method
String selectsql = "select current_day , open_val , high_val , low_val , close_val from historical_data where symbol_name = ?";
List<String[]>resultData = new ArrayList<Stirng[]>();
while(Rset.next())
{
String[] array = new String[5];
array[0] = SgxRset.getString("current_day");
array[1] = SgxRset.getString("open_val");
array[2] = SgxRset.getString("high_val");
array[3] = SgxRset.getString("low_val");
array[4] = SgxRset.getString("close_val");
}
Object[] arrayResultData = resultData.toArray();
Related
I have a Java class which uses BufferedReader to obtain information from a text file, then store the information into an Array called newData . I want to store a certain part of the information to the VegTypes[f] = new VegType(); but I not sure what code should I write here to obtain that part of information.
Without completing this part, I am not able to continue working on another Array Object which is Vegs[i] = new Veg(newData[0], newData[1], newData[2],); for storing information together with VegTypes Array Object.
Below is my code of the Java class:
public class theVegetable {
private Veg[] Vegs;
private VegType[] VegTypes;
public theVegetable() {
int quantity;
int vegQuantity;
String vegLine;
try {
BufferedReader br = new BufferedReader(new FileReader("vegetableInfo.txt"));
quantity = Integer.parseInt(vegLine.readLine());
Vegs = new Veg[quantity];
for (int i = 0; i < quantity; i++) {
vegLine = br.readLine();
String[] newData = vegLine.split(";");
vegQuantity = Integer.parseInt(newData[3]);
//For loop to store information into VegTypes
for (int f = 0; j < vegQuantity; f++) {
VegTypes[f] = new VegType();
}
//Vegs Array Object to store information plus VegTypes
Vegs[i] = new Veg(newData[0], newData[1], newData[2],);
}
br.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
}
}
Below is my vegetableInfo.txt text file:
3
Tomato;class1;Malaysia Tomato;2;MT100A;MT1;200;90;MT20A;MT2;600;80;Malaysia product
Avocado;class2;Europe Avocado;4;EA100A;EA1;300;90;EA14A;EA2;90;80;EA230A;EA3;43;50.9;EA470A;EA4;400;76;Europe product
Cabbage;class3;Malaysia Cabbage;3;MC100A;MC1;500;20;MC49A;MC2;500;50;MC800A;MC3;600;10.3;Malaysia product
The number 3 at the top of the text file is for the int quantity; variable to store the amount.
The kind of information I want the VegTypes[f] = new VegType(); to store are MT100A;MT1;200;90;MT20A;MT2;600;80;, the number 2 besides the Malaysia Tomato are for int vegQuantity; variable. Same thing goes for other vegetables in the text file.
Constructor of my private VegType[] VegTypes; Array Object:
private String vegCode;
private String vegBatch;
private int vegBatchQuantity;
private double vegGrade;
public VegType(String inVegCode, String inVegBatch, int inVegBatchQuantity, double inVegGrade) {
vegCode = inVegCode;
vegBatch = inVegBatch;
vegBatchQuantity = inVegBatchQuantity;
vegGrade = inVegGrade;
}
My Veg Class:
public class Veg {
private String vegetableName;
private String classLevel;
private String productionCountry;
private VegType[] VegTypes;
private String productType;
//Constructor
public Veg(String inVegetableName, String inClassLevel, String inProductionCountry, VegType[] inVegTypes, String inProductType) {
vegetableName = inVegetableName;
classLevel = inClassLevel;
productionCountry = inProductionCountry;
vegType = inVegTypes;
productType = inProductType;
}
public String getVegetableName() {
return vegetableName;
}
public String getClassLevel() {
return classLevel;
}
public String getProductionCountry() {
return productionCountry;
}
public String getProductType() {
return productType;
}
}
This is wrong:
//For loop to store information into VegTypes
for (int f = 0; j < vegQuantity; f++) {
VegTypes[f] = new VegType();
}
you need to use f to adjust the index into your array of fields.
for (int f = 0; j < vegQuantity; f++) {
String vegCode = newLine[f*4 + 4];
String vegBatch = newLine[f*4 + 5];
int vegQuantity = Integer.parse(newLine[f*4 + 6]);
double vegGrade = Double.parse(newLine[f*4 + 7]);
VegTypes[f] = new VegType(vegCode, vegBatch, vegQuantity, vegGrade);
}
class VegType {
private String vegCode;
private String vegBatch;
private int vegBatchQuantity;
private double vegGrade;
public VegType(String inVegCode, String inVegBatch, int inVegBatchQuantity, double inVegGrade) {
vegCode = inVegCode;
vegBatch = inVegBatch;
vegBatchQuantity = inVegBatchQuantity;
vegGrade = inVegGrade;
}
#Override
public String toString() {
return vegCode+" "+vegBatch+" "+vegBatchQuantity+" "+vegGrade;
}
}
class Veg {
private String vegetableName;
private String classLevel;
private String productionCountry;
private VegType[] VegTypes;
private String productType;
//Constructor
public Veg(String inVegetableName, String inClassLevel, String inProductionCountry, VegType[] inVegTypes, String inProductType) {
vegetableName = inVegetableName;
classLevel = inClassLevel;
productionCountry = inProductionCountry;
VegTypes = inVegTypes;
productType = inProductType;
}
#Override
public String toString() {
return vegetableName+" "+classLevel+" "+productionCountry+" "+VegTypes+" "+productType;
}
public String getVegetableName() {
return vegetableName;
}
public String getClassLevel() {
return classLevel;
}
public String getProductionCountry() {
return productionCountry;
}
public String getProductType() {
return productType;
}
}
class TheVegetable {
private Veg[] Vegs;
private VegType[] VegTypes;
public TheVegetable() throws IOException {
int quantity;
int vegQuantity;
String vegLine;
try {
BufferedReader br = new BufferedReader(new FileReader("vegetableInfo.txt"));
quantity = Integer.parseInt(br.readLine());
Vegs = new Veg[quantity];
for (int i=0; i<quantity; i++) {
vegLine = br.readLine();
String[] newData = vegLine.split(";");
vegQuantity = Integer.parseInt(newData[3]);
VegTypes=new VegType[vegQuantity];
for(int j=4, k=0; j<newData.length-1; j+=4, k++) {
VegTypes[k] = new VegType(newData[j], newData[j+1], Integer.parseInt(newData[j+2]), Double.parseDouble(newData[j+3]));
}
Vegs[i]=new Veg(newData[0], newData[1], newData[2], VegTypes, newData[newData.length-1]);
}
br.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
}
}
I want to know how I can iterate to a certain index inside a 2D array, for example how could I fetch the itemQuantity in this code and if there were more than one products how could I fetch all of their quantities.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class practice2 {
public static void main(String[] args) {
List<String[]> inventoryTable = new ArrayList<>();
inventoryTable.add(new String[]{
"Item Name",
"Item Type",
"Quantity",
"Price",
"Expiry Date",
"Date of Purchase"});
String itemName = "oranges";
String itemType = "fruit";
String itemQuantity = "3";
String itemPrice = "2";
String itemExpiryDate = "12/11/2020";
String itemDateOfPurchase = "30/10/2020";
inventoryTable.add(new String[]{
itemName,
itemType,
itemQuantity,
itemPrice,
itemExpiryDate,
itemDateOfPurchase
});
System.out.println(Arrays.deepToString(inventoryTable.toArray()));
}
}
In your case, you have an ArrayList containing String arrays where the third index is the quantity you're looking for.
Using the java "foreach" statement:
//first we iterate through the whole ArrayList obtaining each inventory element
for (String[] inventoryElement : inventoryTable) {
// then we take the third index and we print it out
System.out.println(inventoryElement[3]);
}
Edit: As someone already mention in the comments it's much better to create an object to hold all the information instead of String array.
Example solution:
List<InventoryItem> inventoryTable = new ArrayList<>();
String itemName = "oranges";
String itemType = "fruit";
int itemQuantity = 3;
double itemPrice = 2.0;
String itemExpiryDate = "12/11/2020";
String itemDateOfPurchase = "30/10/2020";
inventoryTable.add(new InventoryItem(itemName, itemType, itemQuantity,
itemPrice, itemExpiryDate, itemDateOfPurchase));
for (InventoryItem inventoryItem : inventoryTable) {
System.out.println(inventoryItem.getPrice());
}
public static class InventoryItem {
private String name, type, expiryDate, dateOfPurchase;
private int quantity;
private double price;
public InventoryItem(String name, String type, int quantity,
double price, String expiryDate,
String dateOfPurchase) {
this.name = name;
this.type = type;
this.quantity = quantity;
this.price = price;
this.expiryDate = expiryDate;
this.dateOfPurchase = dateOfPurchase;
}
public String getName() { return name; }
public double getPrice() { return price; }
public int getQuantity() { return quantity; }
public String getDateOfPurchase() { return dateOfPurchase; }
public String getExpiryDate() { return expiryDate; }
public String getType() { return type; }
#Override
public String toString() {
return "Item: " + name
+ " Type: " + type
+ " Quantity: " + quantity
+ " Price:" + price
+ " Expiry date: " + expiryDate
+ " Date of purchase" + dateOfPurchase;
}
}
You can look at the arraylist as an array. Meaning you can access the indexes [][] but would need to use the get(index)[] instead.
For example how could I fetch the itemQuantity in this code:
inventoryTable.get(1)[2];
would get you the itemQuantity.
And if there were more than one products how could I fetch all of their quantities.
for (int i = 1; i < inventoryTable.size(); i++) {
System.out.println(inventoryTable.get(i)[2]);
}
The question wants me to do:
An array of Finance called financeRecord to store the details
of the payments for each semester.
This is my code
package lab5;
class Student_U extends Student {
public String student_name;
private String studentID;
public int student_age;
private byte currentSemester;
private byte TotalFinanceRecord;
private String cohort;
public Student_U() {
student_name = " ";
studentID = " ";
student_age = 0;
currentSemester = 1;
TotalFinanceRecord = 0;
cohort = " ";
}
public Student_U(String student_name, String studentID, int student_age,
String course, String year,
String section, String subject, String student_name2,
String studentID2, int student_age2,
byte currentSemester, byte totalFinanceRecord, String cohort) {
super(student_name, studentID, student_age, course, year,
section, subject);
student_name = student_name2;
studentID = studentID2;
student_age = student_age2;
this.currentSemester = currentSemester;
TotalFinanceRecord = totalFinanceRecord;
this.cohort = cohort;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public int getStudent_age() {
return student_age;
}
public void setStudent_age(int student_age) {
this.student_age = student_age;
}
public byte getCurrentSemester() {
return currentSemester;
}
public void setCurrentSemester(byte currentSemester) {
this.currentSemester = currentSemester;
}
public byte getTotalFinanceRecord() {
return TotalFinanceRecord;
}
public void setTotalFinanceRecord(byte totalFinanceRecord) {
TotalFinanceRecord = totalFinanceRecord;
}
public String getCohort() {
return cohort;
}
public void setCohort(String cohort) {
this.cohort = cohort;
}
public void initStudent() {
}
public void print() {
System.out.print("Student name: " + student_name + " ");
System.out.print("\nMatric No: " + studentID + " ");
System.out.print("\nAge: " + student_age + " ");
System.out.print("\nCurrent Semester: " + currentSemester + " ");
System.out.print("\nCohort: " + cohort + " ");
System.out.println();
}
}
Please help me fix my code I would appreciate it so much.
This is my lab assignment which needs to be submitted by tomorrow.
You could try this, but it's also better to review standard java concepts (arrays, classes, etc). After, just adapt your code as suitable.
public class Finance extends Student
{
public static void main(String args[])
{
Finance f1 = new Finance("Student_1");
System.out.println(f1);
f1.setPayment(1, 10);
System.out.println(f1);
f1.setPayment(2, 10.77);
System.out.println(f1);
Student s2 = new Student("Student 2");
Finance f2 = new Finance(s2);
f2.setPayment(2, 88.77);
System.out.println(f2);
}
Double finaceRecord[] = new Double[3];
private void initPayment()
{
for(int i=0;i<finaceRecord.length;i++)
{
finaceRecord[i]=0.0;
}
}
public Finance(Student s)
{
super(s.name);
initPayment();
}
public Finance(String name)
{
super(name);
initPayment();
}
//store first or second
public void setPayment(int i, double d)
{
if(d<=0) return;
if(i==1)
{
finaceRecord[i] = d;
}
else
{
finaceRecord[2] = d;
}
finaceRecord[0] = finaceRecord[2] + finaceRecord[1];
}
public String toString()
{
return "name="+super.name+", Total Paid="+finaceRecord[0]+","
+ " Sem1="+finaceRecord[1]+", Sem2="+finaceRecord[2];
}
}
...
public class Student
{
String name;
int Semester;
Student(String name)
{
this.name = name;
this.Semester = 1;
}
}
Ouptut
name=Student_1, Total Paid=0.0, Sem1=0.0, Sem2=0.0
name=Student_1, Total Paid=10.0, Sem1=10.0, Sem2=0.0
name=Student_1, Total Paid=20.77, Sem1=10.0, Sem2=10.77
name=Student 2, Total Paid=88.77, Sem1=0.0, Sem2=88.77
from what I understand you are supposed to create an array of Finance type
Finance []financeRecord = new Finance[totalFinanceRecord];
and then you can access the values of Finance class
financeRecord[indexNumber].methodName();
I take in a file which has a name (table) and the number of seats:
table1 6
table2 2
table3 4
I have an array of class Reservation which will take in the the name and the seat. I am having trouble converting the number of seats into the array. How do i go about doing so?
public class Reservable {
protected String id;
private Reservation[] arrayRes = new Reservation[10];
public Reservable (Scanner fileIn) {
while(fileIn.hasNext()) {
id = fileIn.next();
for(int i = 0; i < arrayRes.length; i++) {
int seat = fileIn.nextInt();
arrayRes[i] = seat;
}
}
}
here is my Reservation class:
public class Reservation {
private String name;
private int timeSlot;
private Reservable myReservable;
public Reservation(String name, int timeSlot) {
this.name = name;
this.timeSlot = timeSlot;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTimeSlot() {
return timeSlot;
}
public void setTimeSlot(int timeSlot) {
this.timeSlot = timeSlot;
}
public Reservable getMyReservable() {
return myReservable;
}
public void setMyReservable(Reservable myReservable) {
this.myReservable = myReservable;
}
public boolean isActive() {
return false;
}
You can read line by line since your file has a reservation by line.
I propose you to have a Reservation constructor with two parameters (name and nbSeat).
A remark : you array of reservation has a fixed size : 10. If you file has more than 10 elements, a ArrayIndexOutOfBoundsException will be risen.
If the number of reservation may be superior to 10 or is variable, you should use a List rather than a array.
protected String id;
private Reservation[] arrayRes = new Reservation[10];
public Reservable (Scanner fileIn) {
int i=0;
while(fileIn.hasNextLine()) {
String line = fileIn.nextLine();
String[] token = line.split("\\s");
String name = token[0];
int nbSeat= Integer.valueOf(token[1)];
// add in your array
Reservation reservation = new Reservation(name,nbSeat);
arrayRes[i] = reservation ;
}
i++;
}
And Reservation :
public class Reservation{
public Reservation(String name, int nbSeat){
this.name=name;
this.nbSeat=nbSeat;
}
}
You need to show us what your Reservation class looks like, but if it uses the conventional getters and setters this is the correct way:
public Reservable (Scanner fileIn) {
while(fileIn.hasNext()) {
for(int i = 0; i < arrayRes.length; i++) {
int seat = fileIn.nextInt();
arrayRes[i].setSeat(seat);
id = fileIn.next();
arrayRes[i].setId(id);
}
}
}
Hello guys i need to output a json string like this, I use Java and Jackson.
{"x_axis": {"type": "datetime"},"series": [
{
"name": "Visitors per month",
"data": [
["2014-01", 71173],
["2014-02", 57624],
["2014-03", 64851],
["2014-04", 60486],
["2014-05", 60500],
["2014-06", 62908],
["2014-07", 64818],
["2014-08", 59961],
["2014-09", 58542],
["2014-10", 22050]
],
} ]}
But I tried everything but I get this, i hope you will understand.
{"series": [
{
"data": [
"{2016-02-12 09:00:00.0, 565}",
"{2016-02-12 09:00:00.0, 565}"
],
"name": "Calls per minute"
}],"x_axis": {
"type": "datetime"}}
UPDATE
The problem is I want to output the data like this
"data": [
["2014-01", 71173],
["2014-02", 57624],
["2014-03", 64851],
["2014-04", 60486],
["2014-05", 60500],
["2014-06", 62908],
["2014-07", 64818],
["2014-08", 59961],
["2014-09", 58542],
["2014-10", 22050]
]
And not like this
"data": [
"{2016-02-12, 565}",
"{2016-02-12, 565}"
]
My Method
public class DataItem
{
public String str;
public String count;
#Override
public String toString() {
return "{"+ str + ", " + count + "}"; // Dit roept de parser aan.
}
public DataItem(String Date, String count)
{
this.str = Date;
this.count = count;
}
}
public LineChart getaverageCallsPerMinuteAsLineChart() throws SQLException {
String query = "select date, averageCallsPerMinute from information where date between now() - INTERVAL 1 DAY and now()";
LineChart linechart = new LineChart();
X_Axis x_axis = new X_Axis("datetime");
linechart.setX_axis(x_axis);
ArrayList<Series> seriesArray = new ArrayList<>();
Series series = new Series();
series.setName("Calls per minute");
List<List<Object>> data = new ArrayList<>();
try {
conn = DBConnection.setDBConnection();
statement = conn.createStatement();
rs = statement.executeQuery(query);
while(rs.next()){
List<Object> dataItems;
String date = rs.getString(1);
String calls = rs.getString(2);
if(date != null || calls != null)
{
DataItem di = new DataItem(date, calls);
dataItems = new ArrayList<Object>(Arrays.asList(di));
data.add(dataItems);
}
}
}catch(Exception e){
System.out.println(e);
}finally{
rs.close();
conn.close();
}
series.setData(data);
seriesArray.add(series);
linechart.setSeries(seriesArray);
return linechart;
}
My classes
public class Series {
private String name;
private List<List<Object>> data;
public Series(){}
public Series(String name, List<List<Object>> data){
this.data = data;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<List<Object>> getData() {
return data;
}
public void setData(List<List<Object>> data) {
this.data = data;
}
}
public class X_Axis {
private String type;
public X_Axis(){}
public X_Axis(String type){
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
public class LineChart {
private X_Axis x_axis;
private ArrayList<Series> series;
public LineChart(){}
public LineChart(X_Axis x_axis, ArrayList<Series> series){
this.x_axis = x_axis;
this.series = series;
}
public X_Axis getX_axis() {
return x_axis;
}
public void setX_axis(X_Axis x_axis) {
this.x_axis = x_axis;
}
public ArrayList<Series> getSeries() {
return series;
}
public void setSeries(ArrayList<Series> series) {
this.series = series;
}
}
#GET
#Path("/CallsPerMinuteAsLineChart")
#Produces(MediaType.APPLICATION_JSON)
public LineChart CallsPerMinute() throws SQLException {
CompanyDB comp = new CompanyDB();
LineChart linechart = comp.getaverageCallsPerMinuteAsLineChart();
return linechart;
}
(I can't post comments yet)
I would say that your :
#Override
public String toString() {
return "{"+ str + ", " + count + "}"; // Dit roept de parser aan.
}
is creating the output with the format "{2016-02-12, 565}" instead of ["2014-01", 71173]. The second one, makes me think that they are 2 objects, a String and an Integer, not just one Object (your DataItem class) with 2 attributes and the toString() function overwritten. I am not very familiar with those libraries but I think you should somehow add to the JSON twice, first time the time and then the value.
Apart from that, I am checking the formating of the Date, I wil come back when I get something.
EDIT: To get the correct format of the Date I found this:
Date utilDate = null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
utilDate = formatter.parse(date);
So I would just change the mask to "yyyy-MM"