I want to write Text View today's date so changed today and the date for the next day?
If you would like to set a TextView so that it will always display the current date, you can do it like this:
Calendar calendar = Calendar.getInstance();
int month = calendar.get(Calendar.MONTH)+1; //we add one because the months actually start at 0
int day = calendar.get(Calendar.DAY_OF_MONTH);
int year = calendar.get(Calendar.YEAR);
TextView textView = (TextView) findViewById(R.id.your_text_view);
String newText = "Today's Date: "+month+"."+day+"."+year;
textView.setText(newText);
Related
During the signup process, I'm trying to implement a code that stores a date value into a string value in the following format: "dd-mm-yyyy".
So, on the onCreate() method part, I declared a DatePicker variable as follows:
DatePicker dob = (DatePicker) findViewById(R.id.dob);
And on the onClick() method part, I wrote a code to convert this DatePicker value into the String.
String entered_dob = dob.toString();
But later when I opened the database I found out that this only returns a value which looks nonsense. How should I implement in order to get what I wanted?
If you want to store your date as a String (which is not a good practice)
DatePicker datePicker ;
SimpleDateFormat dateFormatter ;
Date d ;
String entered_dob ;
datePicker = (DatePicker) findViewById(R.id.dob);
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1;
int year = datePicker.getYear()
d = new Date(year, month, day);
dateFormatter = new SimpleDateFormat("MM-dd-yyyy");
entered_dob = dateFormatter.format(d);
If you want to get the timestamp you can do it like this
Calendar calendar = new GregorianCalendar(year, month, day);
long enterded_dob_ts = calendar.getTimeInMillis();
I would recommend storing the timestamp.
Use the Calendar to create this value. This one you can easily store as long (or string if you really want this)
Checkout this Example
I have 3 strings containing day, month and year values. For example:
String mday = "02";
String mmonth="07";
String myear="2013";
I need to set the DatePicker in my activity to a month from the date above. I do not mean just add 1 to the mmonth value... in case of day 31 I would end up with an invalid date.
So I need some way to increment the date (the valid way) and set the DatePicker with it's value.
I am aware that setting the datePicker with Int values is done like this:
DatePicker datepicker = (DatePicker) findViewById(R.id.datePicker1);
datepicker.init(iYear, iMonth, iDay, null);
// where iYear,iMonth and iDay are integers
But how do I obtain the integer values of day,month and year of an incremented DATE by one month?
So between the first values (strings) and final values of incremented date(integers) what are the steps that I must make?
I assume I would have to use a Calendar.
So my code should look like this:
Integer iYear, iMonth, iDay = 0;
String mday = "02";
String mmonth="07";
String myear="2013";
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(myear), Integer.parseInt(mmonth), Integer.parseInt(mday));
cal.add(Calendar.MONTH, 1);
// here I should get the values from cal inside the iYear, iMonth, iDay, but I do not seem to succeed.
DatePicker datepicker = (DatePicker) findViewById(R.id.datePicker1);
datepicker.init(iYear, iMonth, iDay, null);
if I do:
datepicker.init(cal.YEAR, cal.MONTH, cal.DATE, null);
then application crashes.
What should I do?
How to set this incremented by a month date into my DatePicker?
UPDATE
I changed my test code to this:
Calendar cal = Calendar.getInstance();
cal.set(2013, 05, 23);
cal.add(Calendar.MONTH, 1);
int xxday = cal.get(Calendar.DATE);
int xxmonth = cal.get(Calendar.MONTH);
int xxyear = cal.get(Calendar.YEAR);
datepicker.init(xxyear, xxmonth, xxday, null);
but Now the datePicker is set to one month from NOW instead of one month from the wanted date So instead of (2013-06-23) I have (2013-09-23). I assume it's because of
int xxmonth = cal.get(Calendar.MONTH);
how can I get the real month from a Calendar cal; ?
DatePicker class has a method updateDate(year, month, dayOfMonth) which you can use to set a date in your DatePicker as shown below:
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
datePicker.updateDate(2016, 5, 22);
Calendar month is 0 based. So month 07 is August.
Use the following code to initialize the calendar object if you have a date picker:
Calendar calendar = new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth());
Else hard-code the date parts in the constructor
use this tuto to create your DatePickerDialog then use this code inside DatePickerDialog
https://developer.android.com/guide/topics/ui/dialogs.html
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String mday = "02";
String mmonth="07";
String myear="2013";
//convert them to int
int mDay=Integer.valueOf(mday);
int mMonth=Integer.valueOf(mmonth);
int mYear=Integer.valueOf(myear);
return new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
String d=convertToCompletDate(i2,i1,i);
mListener.onDatePicked(d);
}
},mYear,mMonth,mDay);
}
In Kotlin
Assuming your date is a string. i.e:
var defaultDate = "20/4/2022"
you could use
val datePicker = findViewById<DatePicker>(R.id.date_Picker)
var defaultDate = eventDate.toString().split(Regex("/"))
var dd = defaultDate[0].toInt()
var mm = defaultDate[1].toInt()
var yy = defaultDate[2].toInt()
datePicker.updateDate(yy,mm,dd)
I am getting date and time from DatePicker and TimePicker like:
int dateofmonth = date.getDayOfMonth();
int month = date.getMonth() + 1;
int year = date.getYear();
int hour = time.getCurrentHour();
int minutes = time.getCurrentMinute();
But i want date and time like this format:
Friday, December 14,2012 - 4:30 PM.
Any help?
formate it as you want ....
public void SetMyCustomFormat()
{
// Set the Format type and the CustomFormat string.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "put your formate here ";
}
for more help
http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.customformat.aspx
You could try to use SimpleDateFormat, see SimpleDateFormat
Under the examples section is a date that represents your required format.
You need to create a Date Object first from Calendar, you can do as below:
Calendar cal= Calendar.getInstance();
cal.setTime(new Date());
int dateofmonth = date.getDayOfMonth();
int month = date.getMonth();
int year = date.getYear();
cal.set(dateofmonth, month, year);
Now create a SimpleDateFormat object, with the format, you desire, and format date with that format, by
String formattedDate=simpleDateFormat.format(cal.getTime());
If all you need is formatting a Date object in the current locale, you can use DateFormat:
Calendar date = Calendar.getInstance();
date.set(Calendar.YEAR, picker.getYear());
...
String str = DateFormat.getDateTimeInstance().format(date);
The method getDateTimeInstance() returns the preferred display for the current locale, which is desirable to internationalize your application because different locales have different preferences for the order of the components. For example:
US: Friday, December 14,2012 - 4:30 PM
Italy: Venerdì 14 Dicembre 2012, 16:30
In my app I have a date saved in a remote database that I want the date picker to be set to. I've researched and only found examples of setting the datepicker today's date via Calender java util. Example:
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
How can I use the Calendar to display my date from the database and not today's date? Do you have any suggestions or examples I can follow?
Update:
After experimenting with Calendar I tried to use
// set Date
String eventYear =date.substring(0,4);
String eventDay =date.substring(5,7);
String eventMonth =date.substring(8,10);
//convert string to int for because calendar only takes int: set(int,int)
int month = Integer.parseInt(eventMonth);
final Calendar c = Calendar.getInstance();
mMonth=c.get(c.set(Calendar.MONTH, Calendar.month));
// or mMonth=c.get(Calendar.MONTH, Calendar.month);
Generates error that says cannot convert int to void.
How can I use Calendar to set it to a specific date? According to google's developers site I should be able to do this.
http://developer.android.com/reference/java/util/Calendar.html
example:
set(Calendar.MONTH, Calendar.SEPTEMBER)
I'd like the date to be display in the datepicker from the server as a default value.
U can use the updateDate(year, month, day_of_month);
date picker returns integer values of day, month and year. so the parameters must be integer values. and the integer value for the month jan in the date picker is 0.
i needed to put the date extracted from a database into a datepicker. I wrote the following code and it works.
DatePicker DOB;
SQLiteDatabase db;
DOB=(DatePicker)findViewById(R.id.datePicker1);
db = openOrCreateDatabase("BdaySMS", SQLiteDatabase.CREATE_IF_NECESSARY, null);
Cursor cur = db.rawQuery("select * from BdaySMS where ph='"+pn+"';", null);//pn is the phone no.
if(cur.moveToFirst())
{
name.setText(cur.getString(0));
phone.setText(cur.getString(1));
DOB.updateDate(Integer.parseInt(cur.getString(4)),Integer.parseInt(cur.getString(3)),Integer.parseInt(cur.getString(2)));
message.setText(cur.getString(5));
}
Use JodaTime
Here's a simple example of how I set a DatePicker and TimePicker from a DateTime object, which could be the current date or any date from the past or future (the attribute in this case is called inspected_at):
DatePicker dp = (DatePicker) findViewById(R.id.inspected_at_date);
TimePicker tp = (TimePicker) findViewById(R.id.inspected_at_time);
DateTime inspected_at = DateTime.now().minusYears(1); // Typically pulled from DB.
int year = inspected_at.getYear() ;
int month = inspected_at.getMonthOfYear() - 1; // Need to subtract 1 here.
int day = inspected_at.getDayOfMonth();
int hour = inspected_at.getHourOfDay();
int minutes = inspected_at.getMinuteOfHour();
dp.updateDate(year, month, day);
tp.setCurrentHour(hour);
tp.setCurrentMinute(minutes);
Hope that helps.
JP
I have created an xml layout which is in effect a grid. I need to use it as a calendar and have read about the calendar class but i've been unable to get any of the code it suggests to work.
How would i go about displaying the current year,month and days in the various text views?
This is what i have so far:
Calendar c = Calendar.getInstance();
int month = c.get(Calendar.MONTH);
String month_name = Integer.toString(month);
TextView monthdisp = (TextView) findViewById(R.id.month_disp);
monthdisp.setText(month_name);
setContentView(R.layout.main);
However a null pointer excpetion is generated at monthdisp.setText(month_name);
See the Android Developers guide To get the current year, month and days you'd use the following:
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR); // get the current year
int month = cal.get(Calendar.MONTH); // month...
int day = cal.get(Calendar.DAY_OF_MONTH); // current day in the month
// sets your textview to e.g. 2012/03/15 for today
textview.setText("Year / month / day: "+ year + "/" + month + "/" + day);
Alternatively, you can use SimpleDateFormat like so:
// set-up the desired formatting
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
Date now = cal.getTime(); // set the current datetime in a Date-object
// SimpleDateFormat.format( Date date ) returns a formatted string
// with the predefined format
String mTimeString = sdf.format( now ); // contains yyyy-MM-dd (e.g. 2012-03-15 for March 15, 2012)
TextView textView = new TextView(this);
textView.setText( mTimeString );
The other way around also works:
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String mDateString = "14-05-2012";
Date mDate = sdf.parse( mDateString ); // returns a Date-object
// with date set to May 14, 2012
The other answers here provide good info about the Calendar class and date/time manipulation, but that's not why your code is throwing a NullPointerException:
TextView monthdisp = (TextView) findViewById(R.id.month_disp);
monthdisp.setText(month_name); // NPE here indicates monthdisp is null
Calling findViewById(R.id.month_disp) returned null. This is probably because you haven't inflated your view yet, or month_disp just isn't in your layout.
Typically, view inflation occurs in the Activity.onCreate() method, and you can do it in several ways, for instance setContentView(). Check that you are doing this before calling findViewById(), and that the layout you are inflating actually has that view in it.