I want show time in TextView using below code.
PersianCalendar persianCalendar = new PersianCalendar();
persianCalendar.setTime(mDate);
Calendar cal = Calendar.getInstance();
cal.setTime(mDate);
holder.tvTime.setText(cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE));
Problem occurs when time is for example 12:03 in TextView displayed as 12:3
How can I fix this?
You can format a Date to a String like this (using java.text.SimpleDateFormat)
holder.tvTime.setText(new SimpleDateFormat("H:mm").format(mDate));
The H format specifier means Hour in day (0-23), and the mm means Minute in hour with two digits.
See the documentation for other formatting options.
Use this code to format minute in 2 digits..
holder.tvTime.setText(String.format("%d:%02d",cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE));
Edit - As suggested by #henry.. you can directly go with this.. holder.tvTime.setText(String.format("%d:%02d",cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE));
I had same issue and solved by this.
Probably it is not the best solution, but it worked for me.
PersianCalendar persianCalendar = new PersianCalendar();
persianCalendar.setTime(mDate);
Calendar cal = Calendar.getInstance();
cal.setTime(mDate);
String minuteSelected = String.valueOf(cal.get(Calendar.MINUTE));
String minutWith0 = ((minuteSelected.length() == 1 ? "0" + minuteSelected : minuteSelected));// to get minut "01" insted of "1"
holder.tvTime.setText(cal.get(Calendar.HOUR_OF_DAY)+":"+ minutWith0);
Related
Every time I try to set CalendarView to focus today (use setDate() to set), it always shows the last day which is available in the Calendar (31 November 2100).
But if i set date to another day it's work fine.
CalendarView cal = new CalendarView(this);
cal.setDate(new Date().getTime(),false,true);
CalendarView cal = new CalendarView(this);
cal.setDate(System.currentTimeMillis(),false,true);
or
cal.setDate(Calendar.getInstance().getTimeInMillis(),false,true);
This is waste of time + headache thing ,I really wonder why there are no one here answer this topic (3years ago!) /or why there is no any top search result in Google to answer this simple problem.
It's too sad that I can't never explain my rage with my bad English skill,
So I will just write it here for the other that will face this issue.
To convert DateTime of C# for Calendar View -> You must subtract tick count of this date 1/1/1970
public static long DatetimeToLong(DateTime dt) {
return (long)( dt - new DateTime(1970, 1, 1)).TotalMilliseconds ;
//(dt - (new DateTime(1970, 1, 1) ) );
}
Calendar_View.SetDate( util.DatetimeToLong(DateTime.Now.Date.AddDays(20) ) ,false ,true);
p.s. Conversion from milliseconds to DateTime format
I am facing problem where I am extracting date from Date class in android and using it in my activity, But my problem here is everything works fine but date function returns 25/03/2014 instead of current date.
Now I am confused why it is returning this irrelavent date instead of current date, Also there is no error.
My code:
import java.util.date;
SimpleDateFormat dateformat=new SimpleDateFormat("dd/mm/yyyy");
dateformat.format(new Date());
Please help to know how to get current date.
From SimpleDateFormat API reference,
d day in month
m minute in hour
M month in year
y year
Change dd/mm/yyyy to dd/MM/yyyy for day/month/year format.
Please check Time & Date settings of your emulator, sometimes that causes the issues like those as well.
Use this dude
Date date = new Date(location.getTime());
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
System.out.println("Time: " + dateFormat.format(date));
This is giving me correct time
String ntime = new SimpleDateFormat("hh:mm:ss").format(new Date());
For Date u can use
String ndate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
Does anyone here know what the best way is to calculate the date 2 days in the past?
I've got this piece of code to retrieve the current date:
public static String getDateTime (String Format){
SimpleDateFormat sdf = new SimpleDateFormat(Format);
return sdf.format(new Date());
}
But I want to be able to calculate the date 2 days in the past. So decrease the date with 2 days. Anyone who knows what the best way is to do this?
Thanks in advance
Using Calendar is probably the easiest way. Assuming that you have defined Format as per the question:
// get Now
Calendar cal = Calendar.getInstance();
// go back two days
cal.add(Calendar.DAY_OF_YEAR, -2);
// display
SimpleDateFormat sdf = new SimpleDateFormat(Format);
String string = sdf.format(cal.getTime());
Just use Calendar's add() function:
Calendar c = Calendar.getInstance();
c.setTime(yourDateObject);
c.add(Calendar.DAY_OF_MONTH, -2);
It will automatically change the month, year, etc. if necessary.
Im working on android and parse some XML file
I get some date with this
DateFormat h = new SimpleDateFormat ("hh:mm:ss", Locale.FRANCE);
DateFormat d = new SimpleDateFormat ("yyyy-M-dd'T'hh:mm:ss", Locale.FRANCE);
Date hdebut = h.parse(maString);
Probleme is Date is for Date not hour so it give me a 1 january 1970(start of timestamp right?) a the correct our so i can't compare by using
Date now new Date();
now.after(hdebut);
i have some method to getHours or month but they are decrepetead so i don't know if i can use them or if wa have a better way no to do it
Any idea?
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR);
Try this. There are a lot of constants you can use with Calendar class.
Date is deprecated. You should be using Calendar instead.
Calendar provides working after and before methods as well and should work with pretty much any date you'll get to use :).
date format has this am_pm field but how can put conditions in an if statement?
if(time = "pm") {
*do this* }
something like that. Is that possible?
Thanks for the help guys :)
AM_PM_FIELD is a constant int. I assume you want to see if a time is AM or PM? Check out the SimpleDateFormat, http://developer.android.com/reference/java/text/SimpleDateFormat.html
I haven't tested the following code so don't quote me
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("a");
String amOrPm = df.format(c.getTime());