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);
Related
The date from DateRangePicker displays as: "yyyy-M-d".
But I want it to display the date as: "yyyy-MM-dd".
I have tried out the following code:
#Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth, int yearEnd, int monthOfYearEnd, int dayOfMonthEnd) {
String selection1 = year + "-" + (monthOfYear+1) + "-" + dayOfMonth;
String selection2 = yearEnd + "-" + (monthOfYearEnd+1) + "-" + dayOfMonthEnd;
Long firstDateSelection = Long.parseLong(selection1);
Long secondDateSelection = Long.parseLong(selection2);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = new Date(firstDateSelection);
Date endDate = new Date(secondDateSelection);
formatter.format(startDate); formatter.format(endDate);
But I get NumberFromatException when I run it at "Long firstDateSelection = Long.parseLong(selection1)"?
Help is much appreciated :)
Thanks!
You are getting NumberFormatException because "1234-12-23" is not a valid Long number and you are trying to convert it to a Long here:
Long firstDateSelection = Long.parseLong(selection1);
Since you already have the numbers for day, month and year you can simply format it(no need for SimpleDateFormat):
String dateString = String.format("%1$04d-%2$02d-%3$02d", year, monthOfYear, dayOfMonth);
If you check Javadoc about parseLong(string)
Throws
NumberFormatException
if the string does not contain a parsable long.
As you can see a string with "-" is not a long number and it throws that exception.
If you want to do it correctly you have to create a Calendar, add it the values you are passing by parameters (year, monthOfYear...) with set(...) and then make getTime() to get Date object.
Don't create a Date object directly (new Date...) because its methods setHour, etc. are deprecated.
Once you have that, you will be able to do .format(dateObject).
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);
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 would like to ask if I have a date with dd/mm/yyyy in String format, I want to split it to int year, int month and int day, how can to that.
Other than that, the String date (dd/mm/yyyy) is stored in the SQLite.
you split this way also
String date="dd/mm/yyyy";
String[] items1 = date.split("/");
String date1=items1[0];
String month=items1[1];
String year=items1[2];
This is what I done... Thank you the above answer as a references~~ ^^
String insertDate = EventListAdapter.KEY_DATE;
String[] items1 = insertDate.split("/");
String d1=items1[0];
String m1=items1[1];
String y1=items1[2];
int d = Integer.parseInt(d1);
int m = Integer.parseInt(m1);
int y = Integer.parseInt(y1);
public static void main(String[] args) {
String startDateString = "01/26/2013";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date startDate=null;
String newDateString = null;
try
{
startDate = df.parse(startDateString);
System.out.println("month===>>>"+(startDate.getMonth()+1));
System.out.println("date===>>>"+startDate.getDate());
System.out.println("year===>>>"+(startDate.getYear()+1900));
} catch (Exception e)//(ParseException e)
{
e.printStackTrace();
}
}
Use the Split method of the string object and cast the resulting strings to int
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String)
If possible, you will find date handling easier if you store the Long date value that you get from Calendar.getTimeInMilliseconds
Storing dates as string with no regard to locale is inviting errors later.
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date d = df.parse("String date (dd/mm/yyyy) is stored in the SQLite");
int month = d.getMonth();
int day= d.getDay();
int year = d.getYear();
Dates are very delicate. Usually a date has to be in rererence to a position (timezone).
Either use the classes Java provides like Date and Calendar together with a TimeZone to interpret the String correctly into a real date object, or if you don't care about the position of that date at all you can use String.split() to get an array, check the correct form and call Interger.valueOf on each of the parts.
Split string using "/"
String s= "15/5/2013";
String arr[] =s.split("/");
System.out.println("Date = "+arr[0]);
System.out.println("Month = "+arr[1]);
System.out.println("Year = "+arr[2]);
Output
Date = 15
Month = 5
Year = 2013
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