0

i want to change date with my formulation (in java code in android studio) to another date, but i need date (yyyy-mm-dd) at int variable to use my formulates on it(f.example i want a=yyyy/2 , b= mm+3, c= dd-25 and result for new date is: aaaa/bb/cc), help me to solve that?

these data using for my graph.

and here is my java code:

// generate Dates
Calendar calendar = Calendar.getInstance();
Date d1 = calendar.getTime();
calendar.add(Calendar.DATE, 1);
Date d2 = calendar.getTime();
calendar.add(Calendar.DATE, 1);
Date d3 = calendar.getTime();

GraphView graph = (GraphView) findViewById(R.id.graph);

// you can directly pass Date objects to DataPoint-Constructor
// this will convert the Date to double via Date#getTime()
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
    new DataPoint(d1, 1),
    new DataPoint(d2, 5),
    new DataPoint(d3, 3)
});

graph.addSeries(series);

// set date label formatter
graph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(getActivity()));
graph.getGridLabelRenderer().setNumHorizontalLabels(3); // only 4 because of the space

// set manual x bounds to have nice steps
graph.getViewport().setMinX(d1.getTime());
graph.getViewport().setMaxX(d3.getTime());
graph.getViewport().setXAxisBoundsManual(true);

// as we use dates as labels, the human rounding to nice readable numbers
// is not necessary
graph.getGridLabelRenderer().setHumanRounding(false);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
a.seif
  • 124
  • 2
  • 8
  • you are saying you want the yyyy value as an int and same for MM and dd? – Pier Giorgio Misley Jan 02 '17 at 13:04
  • What is the format you have to what format you want to convert , there are already pre written ones buddy! – Charuක Jan 02 '17 at 13:07
  • Your Java code? The code in the question was taken from http://www.android-graphview.org/dates-as-labels/. It’s a good start, but pretty poor for visualizing what *you* are trying to do. – Ole V.V. Jan 02 '17 at 13:51
  • Rather than `Calendar` I’d look into `java.time.LocalDate`. It seems better suited for the date arithmetic you are suggesting. Use for example `LocalDate.toEpochDay()` to obtain something you can feed into the `DataPoint` constructor. Still stick to `setHumanRounding(false)`. – Ole V.V. Jan 02 '17 at 13:59

1 Answers1

2

Maybe I didn't understand your question, but maybe yes.

I think that what you want is to get the int value of Year, Month and Day of a given date.

If not, let me know and please clarify your question.

Anyway, this is how to do it.

// first initialize a Calendar item
Calendar currentDate = Calendar.getInstance();
// this is the current date, you can also set up a Calendar with a custom date and time.
// now get the values:
int years = currentDate.get(Calendar.YEAR);
int months = currentDate.get(Calendar.MONTH);
int days = currentDate.get(Calendar.DAY_OF_MONTH);

You can also convert a Date object to a Calendar object

Ref

Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66