Skip to content

Instantly share code, notes, and snippets.

@codingwithsara
Created November 9, 2018 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingwithsara/baaf88e60826e41fae0da36c398b0cfa to your computer and use it in GitHub Desktop.
Save codingwithsara/baaf88e60826e41fae0da36c398b0cfa to your computer and use it in GitHub Desktop.
Java (Android Studio) Tutorial – Calendar, SimpleDateFormat –
package practice.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class main extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView yLabel = (TextView)findViewById(R.id.yLabel);
TextView mLabel = (TextView)findViewById(R.id.mLabel);
TextView dLabel = (TextView)findViewById(R.id.dLabel);
TextView eLabel = (TextView)findViewById(R.id.eLabel);
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MMMM/d/E", Locale.US); // Set your locale!
String strDate = sdf.format(cal.getTime());
String[] values = strDate.split("/",0);
// Confirm
// for (int i = 0; i < values.length; i++) {
// Log.v("CHECK_DATE", values[i]);
// }
yLabel.setText(values[0]);
mLabel.setText(values[1]);
dLabel.setText(values[2]);
eLabel.setText(values[3]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment