The GregorianCalendar constructor asks for the following:
GregorianCalendar(int year, int month, int dayOfMonth);
How can I extract the year, month, and day from an object I have created. Right now I'm using object.YEAR, object.MONTH, and object.DAY_OF_MONTH, but that does not seem to be giving me the right numbers.
Thanks.
Here I get a date based on which calendar date a user clicks. The user can then enter some information into that date which is stored in a HashMap keyed on GregorianCalendar.
cal.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
selectedDate = new GregorianCalendar(year, month, dayOfMonth);
Here I am trying to write the date from the GregorianCalendar year, month, and day parameters to a file to be used later.
private void writeToFile() {
try
{
PrintWriter writer = new PrintWriter(file, "UTF-8");
for (GregorianCalendar gregObject: recipes.keySet()) {
String[] value = recipes.get(gregObject);
int y = gregObject.YEAR;
int m = gregObject.MONTH;
int d = gregObject.DAY_OF_MONTH;
writer.printf("%d %d %d ", y, m, d);
Here is how I read from the file. When I read from the file I get the numbers 1,2,5 for year, month, and date which is wrong. The rest of the information read is correct.
try
{
Scanner getLine = new Scanner(file);
Scanner tokenizer;
while (getLine.hasNextLine()) {
String line = getLine.nextLine();
tokenizer = new Scanner(line);
System.out.println(line);
while (tokenizer.hasNextInt()) {
int y1 = tokenizer.nextInt();
int m1 = tokenizer.nextInt();
int d1 = tokenizer.nextInt();
Obviously I think I am writing the year, month, and day wrongly to the file, so I'm trying to figure out the correct way to extract the year, month, and day from a GregorianCalendar object.
Hope following helps: I am using 2015/06/10 as input. Please note month values are 0 (Jan) - 11 (Dec).
package demo;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* Create on 4/3/16.
*/
public class TestCalendar {
public static void main(String [] args){
Calendar cal = new GregorianCalendar(2015,05,10); // Month values are 0(Jan) - 11 (Dec). So for June it is 05
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH); // 0 - 11
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
// Following must output 2015/06/10
System.out.printf("Provided date is %4d/%02d/%02d", year, month+1, dayOfMonth);
}
}