How can I show today's date in an android activity? - android

I am currently working on my project for my professional course. I am implementing a digital diary.
I would be having a diary page (a multiline textbox maybe) And a TextView on top to show the current DATE (without time). How do I show a NON-editabale textbox with today's date shown in it.

In order to get the current date and time use the below Links and iin order make it non editable in XML file make editable = false also focusable = false.
LINK1
LINK2

its simple
Date dt = new Date();
int date = dt.getDate();
int month = dt.getMonth()+1;
int year = dt.getYear();
year += 1900;
int day = dt.getDay();
String[] days = { "Sunday", "Monday", "Tuesday","WednesDay", "Thursday", "Friday", "Saterday" };
String curDate = date + "/" + month + "/" + year + " " + days[day];

Use this code :
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
It 'll print :
Feb 5, 2013, 12:35:46PM

Related

How to convert Date in Android

I want show date into my application, and i receive this data from server with below json :
"date": "2016-08-01 19:55:16"
and i set into textview with below code :
((DataViewHolder) holder).main_dateTime.setText(Html.fromHtml(mDateSet.get(position).getDate()));
I want convert this date to jalali/shamsi . but i don't know how to convert this date and set into textview !
Can you help me for this issue?
Use this https://github.com/amirmehdizadeh/JalaliCalendar to get the Jalali date
first get the year, month and day from your date like
String date = "2016-08-01 19:55:16";
String[] parts = date.split(" ");
String datePart = parts[0];
String timePart = parts[1];
int year;
int month;
int day;
String[] dateParts = datePart.split("-");
year = Integer.parseInt( dateParts[0]);
month = Integer.parseInt( dateParts[1]);
day = Integer.parseInt( dateParts[2]);
then create the Object to pass to that library
JalaliCalendar.YearMonthDate georgianDate = new JalaliCalendar.YearMonthDate(year,month,day);
and then call its method that convert from Georgian date to Jalali Date
JalaliCalendar.YearMonthDate JalaliDate = JalaliCalendar.gregorianToJalali(georgianDate);
And Finally append the date with time to show in text view
String jalaliDateTime = JalaliDate.toString() + " " + timePart;
textView.setText(jalaliDateTime);

How to get given date format in phonegap?

I am new to phone gap.
I want to send date like a specific format to server.
I face problem to get the date format of given string.
For example i get the string like
20/04/2014 or
04/20/2014 or
2014/04/20 or
20 Apr 2014 ....
I mean i get the same date in different formats.i need to convert the date to a specific format,if it it is any format.
If any one know the solution.
Please help me.
Thanks in advance.
There is a related plugin cordova-plugin-globalization, I'm not sure whether it is what you need, but why not have a try?
https://github.com/apache/cordova-plugin-globalization/blob/master/doc/index.md
Hope this is helpful to you.
You can use,
var currentDate = new Date();
document.write(currentDate);
To return the current date.
(OR)
The Hard way...
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;//January is 0, so always add + 1
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm}
var today = mm+'/'+dd+'/'+yyyy;
document.write(today);
If you used this then you can format the date to the way you wanted.
Working Demo
It will give you in one format. check the fiddle with your example dates.
var d = new Date();
var c = new Date('20 Apr 2014');
alert(formatDate(c)); //This will give you proper one format date
alert(formatDate(d)); // This returns today's date
function formatDate(d)
{
var month = d.getMonth();
var day = d.getDate();
month = month + 1;
month = month + "";
if (month.length == 1)
{
month = "0" + month;
}
day = day + "";
if (day.length == 1)
{
day = "0" + day;
}
return month + '-' + day + '-' + d.getFullYear();
}

DateTime incorrect month and how convert to dd/mm/yyyy in database

I want to let the user choose a Date from a DatePicker and store it into database, and then convert it to dd/mm/yyyy format.
dp =(DatePicker)findViewById(R.id.DateActivity);
setDate.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
requestDate.setText("Day" + dp.getDayOfMonth() + " Month " + (dp.getMonth() + 1)+ " Year " + dp.getYear());
}
});
The output is Day 9 Month 2 Year 2014.
The output months is less by one for every month I choose so I add 1 to the month. Why this is the case?
But the main problem is how do I convert it to 09022014 and store it in the database? If the db has the format dd/mm/yyyy does it means it will not accept my output?
java.util.Calendar treats the months as a zero-based list, so the DatePicker follows this convention as well (see the documentation for DatePicker.init). It's confusing, but that's just the way Java does it (for now, at least). Adding 1 like you're doing will work just fine.
As for converting the date, you can use the SimpleDateFormat class to format the date however you like. For what you said, the format pattern string would be "ddMMyyyy". See the sample code below:
// Build Calendar object from DatePicker fields.
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, myDatePicker.getMonth());
cal.set(Calendar.DAY_OF_MONTH, myDatePicker.getDayOfMonth());
cal.set(Calendar.YEAR, myDatePicker.getYear());
// Convert date to desired format.
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
String dateString = sdf.format(cal.getTime());
int dateInt = Integer.parseInt(dateString); // if you want it as an int instead
You may want to consider storing the date as 20140209 ("yyyyMMdd", year-month-day) instead for sorting purposes, as that will naturally allow it to be sorted chronologically.
For your second problem (putting it into the database) you can simply create a string like this:
String todb = dp.getDayOfMonth() + "/" + (dp.getMonth() + 1)+ "/" + dp.getYear();
Which will return a string like 09/21/2014.
Now, as for your first problem (the month being off by one), I believe this stems from a CalendarView.OnDateChangeListener. It says:
month The month that was set [0-11].
I would bet that this was also implemented on DatePicker's (or you're using the CalendarView with the DatePicker), so January is 0 and December is 11. So your way of changing the month by 1 is a perfectly fine way to do it.

Passing a DatePicker value to an Intent

I have a simple form with a DatePicker component on it. The user can enter their date of birth and then submit the form. Once submitted they are shown a review of what they entered into the form. I have every other component on the form working apart from the DatePicker. I am successfully passing a value from the DatePicker into my Intent but for some reason I am getting a very strange, undesired date...
Does anyone know how I can retrieve the actual values the user put in? Below is a screen of what I am getting in the emulator.
This is the part of my code in my onCreate method where I put the date info into the intent
Date myDate = new Date(date.getDayOfMonth(), date.getMonth(), date.getYear());
i.putExtra("dob", myDate.toString());
And then in the activity to show the results this is the code where I display the information pulled from the intent.
dob.setText("Their date of birth is: " + extras.get("dob"));
As you can see I do successfully get output, it's just not the date that I select on the DatePicker itself. I get no runtime errors at all. It's a formatting thing I think. Not sure though so any feedback is appreciated!
I corrected the issue. Rather then creating a Date object and passing it into my intent I decided to retrieve the values from the DatePicker and storing them in integer variables.
int day = date.getDayOfMonth();
int month = date.getMonth();
int year = date.getYear();
i.putExtra("day", day);
i.putExtra("month", month);
i.putExtra("year", year);
Following this I then retrieved the values from the Intent in the new Activity, formatted them and displayed them in a TextView.
int day = extras.getInt("day");
int month =extras.getInt("month");
int year = extras.getInt("year");
dob.setText("Their date of birth is: " + String.valueOf(day) + "/" + String.valueOf(month + 1) + "/" + String.valueOf(year));
I had to add 1 to the month as for some reason If I chose January it would output 0.

How to format Android date without year and display month and day according to local settings? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Android get current time and date
From this Format date without year I know how to get rid of year.
But I also want to formate month and day.
if user set 09/24/2012, it displays 09/24;
if user set 24/09/2012, it displays 24/09.
And I also wanna support l18n.
Maybe the solution is combine DateFormat and DateUtils, but I don't know how to do it.
First split the information into variables, then just display as needed.
public String formatDate(String date)
{
int index1, index2;
index1 = date.indexOf('/');
index2 = date.lastIndexOf('/');
String str1, str2, str3;
str1 = date.substring(0, index1);
str2 = date.substring(index1 + 1, index2);
str3 = date.substring(index2);
// Presuming that str1 is the month and str2 is the day ...
return str1 + "/" + str2;
}

Categories

Resources