TheAlex TheAlex - 1 year ago 84
Android Question

Using Date to select a string - android studio - java

I'm trying to use a Date String to select a string to be displayed in my android app.

The Code im using to GET Date:

Calendar TextCalendar = Calendar.getInstance();
SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
String TextDate = DateFormat.format(TextCalendar.getTime());

The Code im imagining I need to select strings:

TextView MyTextView = (TextView) findViewByID(;
MyTextView.setText ( "DateVariable" + "Comment" )

This code I imagine not to work because I tried but, the bit with "DateVariable" And "Comment" means I could have Strings in Strings.xml Named Each Date but I would have 2 Strings for each day because there is a TITLE String and A Comment String, so i could imagine the string names looking like this

<string name="2016-02-28-TITLE">TESTING20160228</string>
<string name="2016-02-28-COMMENT">TESTING20160228Comment</string>
<string name="2016-02-29-TITLE">....
and so on

If anyone has a better suggestion on how to proceed with this it would be greatly appreciated.

Thanks ~Alexander

Answer Source

Try this:

String s = TextDate + "-Comment";
getString(getResources().getIdentifier(s, "string", getPackageName()));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download