Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_YEAR);
Android app, give day a number
Question
Right I'm very noobie when it comes to Android, I have built a couple of basic applications but nothing major.
What I'm trying to do is create a quote of the day application where by when you load up the application it displays a quote (listed in a string) depending on what day of the year it is.
I have the main code for displaying a quote from a string done. and this works and I can select which quote to show by changing entries in the code or I have written some code to display the quote at random.
The next step is to display a quote depending on the date. For this to happen I'm guessing I need to utilized the built in clock/calendar of the phone. this is where i come unstuck. I have create the local variable daynumber
but this has to be populated somehow pulling the number from the clock.
My questions are these:
1 Am I going about this the right way. Have I got the logic right.
2 If not how would I do this
3 How would I get the phone to pull the day of the year and interpret this.
In short what I'm basically asking for is to turn a year into 365 days 1 is jan 1st, 2 is jan 2nd, 365 is dec 31st
Here is the code I have :
package com.quote.app;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private String[] myString;
private static final Random rgenerator = new Random();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
myString = res.getStringArray(R.array.myArray);
String q = myString[daynumber];
TextView tv = (TextView) findViewById(R.id.text1);
tv.setText(q);
}
}
Solution
OTHER TIPS
Have a look at the Calendar Class. You can get today's date with the following (taken from the reference linked)
Calendar rightNow = Calendar.getInstance()
You can also get the number for the current day (January 1st is 1, 2nd is 2 and so on).