Using SimpleDateFormat to format minutes to display in 2 digits - android

I am trying to make my countdown timer show the seconds as 2 digits instead of one.
Example: 02 instead of 2.
I am using the code below, but I am not having any luck with the formatting.
long a = ((millisUntilFinished % 60000) / 1000);
String b = Long.toString(a);
SimpleDateFormat myFormat = new SimpleDateFormat("HH:mm:ss:");
String time = myFormat.format(b);

The above code should work but if possible try to have your a value in milliseconds. The below code is working fine for me.
long a = 1395886625000L;
SimpleDateFormat myFormat = new SimpleDateFormat("HH:mm:ss");
String time = myFormat.format(a);

Related

Changing Textview Dynamicly after a certain period of Days

I'm quite new to Android and I want to change (I know how to set it dynamically but....)Textview Dynamically after a certain period of Days,but int the meantime keep the text posted unless changed by user or if a certain days have been reached by my program.
Well Basically I have the user to input a Date then i want my program to count days and as long as the user nor my program have intervene with the textview it will show the same text and backround color until the opposite is taken action.
here is my code:
String CurrentDate = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
String FinalDate = TvDateOfService.getText().toString();
SimpleDateFormat dates = new SimpleDateFormat("dd/MM/yyyy");
Date dateinput;
Date datetwra;
//Setting dates
try {
dateinput = dates.parse(FinalDate);
datetwra = dates.parse(CurrentDate);
//Comparing dates
long difference = Math.abs(dateinput.getTime() - datetwra.getTime());
long differenceDates = difference / (24 * 60 * 60 * 1000);
//Convert long to String
String dayDifference = Long.toString(differenceDates);
//Calendar Instance to Add 89 Days for Service Period Between Dates!!
Calendar cal1 = Calendar.getInstance();
cal1.add(Calendar.DATE, 90);
Date ServicePeriod = cal1.getTime();
Calendar cal2 = Calendar.getInstance();
cal2.add(Calendar.DATE, 80);
Date DaysBeforeEnd = cal2.getTime();
do {
tvStatus.setBackground(getApplicationContext().getResources().getDrawable(R.drawable.textview_backround_green));
}while (dateinput.before(DaysBeforeEnd));
do {
tvStatus.setText("something");
tvStatus.setBackground(getApplicationContext().getResources().getDrawable(R.drawable.textview_backround_yellow));
}while (dateinput.after(DaysBeforeEnd));
do {
tvStatus.setText("something else");
tvStatus.setBackground(getApplicationContext().getResources().getDrawable(R.drawable.textview_backround_red));
}while (dateinput.after(ServicePeriod));
It looks like you're trying to use a for loop and wait a certain number of days - it would be most beneficial to the phone's battery if you used AlarmManager
https://developer.android.com/trai.../scheduling/alarms.html

Android, SimpleDateFormat not working

I'm trying to parse time only but the app code includes the date and the year.
here is my code:
simpleDateFormat2 = new SimpleDateFormat("HH:mm");
int h = Integer.parseInt(traffic.alarmClocks.get(0).get(ApplicationConstants.HOUR));
int m = Integer.parseInt(traffic.alarmClocks.get(0).get(ApplicationConstants.MINUTE));
String datex2 = h + ":" + m;
Date storedalarm = simpleDateFormat2.parse(datex2);
output of the datex2 is : 4:56
Output of StoredAlarm is this: http://imgur.com/a/UVpbh
The output of the datex2 is correct, but I need to make it into date because I am going to use it to compare times.
you just need to compare times you could very easily combine hours and minutes this way:
int time = hours * 60 + minutes;
then you could just compare 2 integers.
or if you really want a Date object, you could initialize it with year, month and date to 0, and just pass hours and minutes
Date storedalarm = new Date(0, 0, 0, h, m);
in order to show just hours and minutes from your Date object you can use the same SimpleDateFormat you instantiated before
String formattedDate = simpleDateFormat2.format(storedalarm);

how to convert TimeUnit to seconds or hours?

I want to convert TimeUnit to seconds or hours , I've asked a lot but do not get a satisfactory answer.
I've read on http://developer.android.com/reference/java/util/concurrent/TimeUnit.html#toHours(long) but I don't understand to use
example:
String strfileDate = "2012-04-19 15:15:00";
DateFormat formatter2 = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date da =formatter2.parse(strFileDate);
long diffInMs = da.getTime() - new Date(System.currentTimeMillis()).getTime();
long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMs);
I want to convert valuse of "diffInSec" to Seconds, hours
convert day to hours, should be TimeUnit.DAYS.toHours(days).
for other type, change Enum value for source time unit.

how to get correct date diffrence

i want to compare a date with the current date and do something if the difference is 2 months or 6 or a year .. but i have a problem how to get the correct difference for example if the current month is 02 2015 and the other month is 10 2014 i will get 8 in difference but the actual difference is 4 .. how to do it ?
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat d = new SimpleDateFormat("dd");
SimpleDateFormat m = new SimpleDateFormat("MM");
SimpleDateFormat ye = new SimpleDateFormat("yyyy");
String day = d.format(c.getTime());
String month = m.format(c.getTime());
String year = ye.format(c.getTime());
int d1=Integer.parseInt(day);
int m1=Integer.parseInt(month);
int d2=25;
int m2=02;
int diff=d1-d2;
String s=String.valueOf(diff);
You are calculating your difference between two int, so it can't work.
You should calculate it between two dates or two long (in secondes or milliseconds)
long oneDay, today, delay;
oneDay = 1000*3600*24; //number of milliseconds in a day
today = Calendar.getInstance().getTimeInMillis();
delay = (TheDateYouWantToCompare - today)/oneDay;
if (delay >= 60*oneDay) { //more than 2 months
//your code
}else{
//your code
}
If TheDateYouWantToCompare and today are dates, it's almost the same :
delay = (TheDateYouWantToCompare.getTime() - today.getTime())/oneDay;
Edit :
Here it is how to get time in milliseconds.
String DateString = "31-12-2015";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date myDate = sdf.parse(DateString);
long timeInMilliseconds = myDate.getTime();
You could just use the difference in milliseconds between the 2 dates. Pre-compute the differences you need/want as constants and compare to the delta you have, for example:
static final long DAY = 1000 * 60 * 60 * 25;
static final long MONTH = DAY * 30;
...
int diff = d1 - d2;
if(diff > MONTH) {
//more than a month difference
}
If you need something more complex you should perhaps use a library such as Joda Time which will give a more comprehensive set of features to work with time.

How do I print just the milliseconds (time between last second and next second) of a Date object?

I am building a basic logging utility class and would like to log the date and time down to the millisecond of when the log entry is created. I'm currently using:
Date d = new Date();
String dateToOutput = DateFormat.format( "MM-dd hh:mm:ss", d )
which gives me '05-23 09:05:47'. I would like it to give me the milliseconds of when the log entry is created also and it does not appear that the DateFormat class supports millisecond retrieval.
Like the format "MM-dd hh:mm:ss:zzz" giving '05-23 09:05:47.447'.
Is it possible to do this using the DateFormat class (or a class like DateFormat)? I recognize it is possible to create another date removing the milliseconds part of this date and then subtracting the two and printing the difference but that's just silly. (:
Try this
Date d = new Date();
SimpleDateFormat sdf=new SimpleDateFormat("MM-dd hh:mm:ss SSS");
String dateToOutput = sdf.format(d);
While I think it's silly the DateFormat class doesn't allow easy formatted output of milliseconds, I realized that obtaining the milliseconds from the timestamp is actually quite easy. Every date object is representing a timestamp in milliseconds since 00:00 January 1, 1970 so the timestamp modulo 1000 gives the milliseconds.
I did
Date d = new Date();
String dateToOutput = DateFormat.format( "MM-dd hh:mm:ss", d );
dateToOutput += "." + d.getTime() % 1000;
which, while not ideal, works fine and gives me '05-23 09:05:47.447'.

Categories

Resources