Basically I need to show the current date in my text view then when I click the date it will open the date picker. I already solved the problem but it doesn't show me the current date. when I click the text view it will show me the date picker then I have to select the date. how can I get the current date on my text view?
////set the Current Date in Textview
dateView1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getContext(),android.R.style.Theme_DeviceDefault_Dialog,setListener,year,month,day);
dialog.show();
}
});
setListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month + 1;
String date = month + "-" + day + "-" + year;
dateView1.setText(date);
dateView1.setText(date);
}
};
Click Here To see the image, what i have wanted to is show current date here.
Try this
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
public class Main{
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy.MM.dd");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now));
}
}
for android studio
dateView1.setText(dtf.format(now));
Same theme -> https://stackoverflow.com/a/5369753/16300574
Related
heres my code
irthdate = findViewById(R.id.birthdate);
date = findViewById(R.id.date);
birthdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
calendar=Calendar.getInstance();
year= calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH);
dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH);
datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
displaydate=format.format(new Date());
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
date.setText(displaydate);
}
},year,month,dayOfMonth);
datePickerDialog.show();
I've just modified your code. It's not tested. But hope it helps.
birthdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
String displaydate = year + "-" + month + "-" + day;
date.setText(displaydate);
}
}, year, month, dayOfMonth);
datePickerDialog.show();
}
}
I have a working app where user select an option from Spinner the datepicker auto open user select the date and view data. However, I am trying to include an Imagebutton(calendar) which opens a date picker and than on selection of date data should load. The process should be : select Option from spinner -> select date from datepicker by clicking Imagebutton -> load/display data.
this is the imagebutton onclick event:
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Calendar cldr = Calendar.getInstance();
int year = cldr.get(Calendar.YEAR);
int month = cldr.get(Calendar.MONTH);
int dayOfMonth = cldr.get(Calendar.DAY_OF_MONTH);
// date picker dialog
datePickerDialog = new DatePickerDialog(Dashboard.this,
AlertDialog.THEME_DEVICE_DEFAULT_DARK, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Calendar calendar1 = Calendar.getInstance();
calendar1.set(year, month, dayOfMonth);
Date chosendate = calendar1.getTime();
calendar1.set(year, month, dayOfMonth);
chosendate = calendar1.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",
Locale.JAPAN);
franchiseDate = formatter.format(chosendate);
}
}, year, month, dayOfMonth);
datePickerDialog.show();
datePickerDialog.getDatePicker().setMaxDate(cldr.getTimeInMillis());
}
this is the spinner onItemSelected event:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int
position, long l) {
selectedItem = spinner.getSelectedItem().toString();
selectedPosition = spinner.getSelectedItemPosition();
if (selectedItem.equals("Revenue by Centre")) {
// String centerDate = franchiseDate;
Bundle bundle = new Bundle();
bundle.putString("authToken", authToken);
bundle.putString("centreDate", franchiseDate);
spinner.setEnabled(false);
Centre centre = new Centre();
centre.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.multiFrame, Centre.class,
bundle);
fragmentTransaction.commit();
}
Data is displayed in fragment, date is also a var that is passed to fragment class. If I am including spinner.setOnItemSelectedListener in imageButton.setOnClickListener event than data is loading after I select new date ie select a date no data again select a date data is loading for previously selected date.
Resolved....call the fragment in onDateSet
I want to pass the data from the API (called using Retrofit) to calendar date onClick() event (onSelectedDayChange).
public class MainActivity extends AppCompatActivity {
List<APIData> dataList;
private CalendarView mCalendarView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCalendarView= findViewById(R.id.calendarView);
Calendar rightNow = Calendar.getInstance();
mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
#Override
public void onSelectedDayChange(#NonNull CalendarView view, int year, int month, int dayOfMonth) {
month = 1+month;
Toast.makeText(getApplicationContext(), ""+year+"-"+month+"-"+dayOfMonth,Toast.LENGTH_SHORT).show();
}
});
APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
Call<APIData> call = apiInterface.gethistorydetails("s10-1", "2018-08-01");
call.enqueue(new Callback<APIData>() {
#Override
public void onResponse(Call<APIData> call, Response<APIData> response) {
Log.d("TAG",response.code()+"");
APIData apiData = response.body();
}
#Override
public void onFailure(Call<APIData> call, Throwable t) {
}
});
}
}
As you can see in this pic, there are 4 different fields where I need to show data called from API. So whenever I click any dates on the calendar, the respective data from API should fill up the fields.
Assuming you are having all the 4 dates from APIData object. Convert the date(2018-09-09) into timeInMillis and set the date into calendarView on click of respective date fields:-
bus_arrival.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCalendarView.setDate(timeInMillis);
}
});
check_in.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCalendarView.setDate(timeInMillis);
}
});
In this use case you have to call the api every time whenever you change the date in calendar, This api will give you data in its response, simply set this data in your fields.
There are few steps to follow.
1- In datePicker dailog's onSelectedDayChange method you are getting "int year, int month, int dayOfMonth". Convert these fields into that date format that your API can understand.
To know how to convert these fields into date format follow this link
Creating java date object from year,month,day
2- Now call this APi , this api will return you result of those 4 fields in its response.
3- Simply display this data on those fields.
You code will be liked that
#Override
public void onSelectedDayChange(#NonNull CalendarView view, int year, int month, int dayOfMonth) {
Calendar cal= Calendar.getInstance();
cal.set(year, month - 1, dayOfMonth, 0, 0);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String date=format.formate(cal.getTime());
APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
Call<APIData> call = apiInterface.gethistorydetails("s10-1", date);
call.enqueue(new Callback<APIData>() {
#Override
public void onResponse(Call<APIData> call, Response<APIData> response) {
Log.d("TAG",response.code()+"");
APIData apiData = response.body();
// Here you will get data from this api and display it on your required fields
}
#Override
public void onFailure(Call<APIData> call, Throwable t) {
}
});
I just upgraded to Android Studio 3.1.1 and now I can't find the time nor the date pickers.
I'm running on a Linux machine, and the upgrade was suggested by Android Studio and I just clicked OK.
Below is a screenshot of my palette:
Any idea where I can find them? I'm looking for these two guys:
Thanks!
I think what are you looking for is DatePickerDialog and TimePickerDialog.
You can add it programmatically. In this example onclick edittext event.
For DatePickerDialog :
final Calendar myCalendar = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String myFormat = "dd/MM/yyyy"; //set date format
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
YourDateEditText.setText(sdf.format(myCalendar.getTime()));
}
};
YourDateEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DatePickerDialog(YourActivity.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
For TimePickerDialog :
YourTimeEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(YourActivity.this, new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
YourTimeEditText.setText( selectedHour + ":" + selectedMinute);
}
}, hour, minute, true);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
I am trying to capture a date from date picker dialog with the custom format like dd-MMM-yyyy(10-aug-1982). I have used the following code, but that only accept integer value as month not string. Please see my commented code that is i want to send from date picker to Edit Text.
Please anyone guide that how can i return the month as string. Thanks in advance.Following is my code.
XML
<EditText
android:id="#+id/date"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#d4d4d4"
android:inputType="none"
android:textIsSelectable="true"
android:hint="Select Date..."
android:padding="15dp"
android:textColor="#897"
android:textColorHint="#090"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
code
package com.example.raufabid.mycalendar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
EditText date;
DatePickerDialog datePickerDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
date = (EditText) findViewById(R.id.date);
date.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
// SimpleDateFormat mMonth = new SimpleDateFormat("MMM");
// String Month_name = mMonth.format(c);
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
date.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
});
}
}
EDIT:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate the date picker and a button
date = (EditText) findViewById(R.id.date);
bbc = (EditText) findViewById(R.id.editText2);
// perform click event on edit text
date.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
try {
c.setTime(new SimpleDateFormat("MMM").parse("July"));
int mMonth = c.get(Calendar.MONTH) + 1;
//String mMonth = c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// SimpleDateFormat mMonth = new SimpleDateFormat("MMM");
// String Month_name = mMonth.format(c);
// date picker dialog
datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
date.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
} catch (ParseException e) {
e.printStackTrace();
}
}
});
}
}
Change to the following
// date picker dialog
datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth){
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
String dateString = format.format(calendar.getTime());
date.setText(dateString);
}
}, mYear, mMonth, mDay);
Use "EEEE dd" in place of "dd" to get name of the day.
Like Friday 25-Oct-2019.
// date picker dialog
datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth)
{
Calendar calendar = Calendar.getInstance();
c.set(year , monthOfYear , dayOfMonth);
SimpleDateFormat format = new SimpleDateFormat("EEEE dd-MMM-yyyy");
String datestring=format.format(calendar.getTime());
editText.setText(datestring);
}
}, mYear, mMonth, mDay);