How do I hardcode a specific default time [ANDROID]? - android

I am setting some default hardcoded times. I am looking to hardcode 6pm as my default time. I have the code as follows:
Calendar mDate = Calendar.getInstance();
SimpleDateFormat mDateFormat = new SimpleDateFormat("EEEE, MMMM d, yyyy");
mydatetime = mDate.getTime();
For mydatetime I was looking to hardcode 5pm as the date time, but not sure how to go about it? any ideas?

You can set time to Calendar object as below:
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
Use Calendar.HOUR_OF_DAY for 24 hour format.
Or: Calendar.HOUR for 12 hour format with calendar.set(Calendar.AM_PM, Calendar.PM); for setting AM or PM.
In your case you can use as below:
Calendar mDate = Calendar.getInstance();
mDate.set(Calendar.HOUR, 5);
mDate.set(Calendar.MINUTE, 0);
mDate.set(Calendar.AM_PM, Calendar.PM);
SimpleDateFormat mDateFormat = new SimpleDateFormat("EEEE, MMMM d, yyyy, hh:mm a");
mydatetime = mDate.getTime();

Related

Getting exact time in ( HH:mm:sss a) format from milliseconds

I have to print time in my app in HH:mm:sss AM/PM from milliseconds which is 24 Hours later time .
so to get 24 hours later time to current time i used the code as
public Date roundToNext24Hour() {
Date date = new Date();
Calendar c = Calendar.getInstance();
c = new GregorianCalendar();
c.setTime(date);
c.add(Calendar.HOUR_OF_DAY, 24);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
return c.getTime();
}
now i convert this time in HH:mm:sss am/pm format
SimpleDateFormat df = new SimpleDateFormat("HH:mm:sss a");
timeString = df.format(Utility.roundToNext24Hour())
but problem is timestring is always returning time which is 12 hour later not 24 hour later.
please help..
Try below code.
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
String str = df.format(date);

Date string dose not get parse

I have a string in date format which I want to parse in date object. I want to set the time as string in calendar object and want to set an alert at the same time.
For this I tried to parse the string into date but nothing happens. The time dose not get set.
When I did debug to check if the time get's set in calendar or not, then found that it goes directly at the end of method after the parsing.
Date date = df.parse(alertDateTest);
I tried to parse the string with this format :
SimpleDateFormat df = new SimpleDateFormat("d MMM yyyy");
But it gives invalid format exception. Which format should I use to parse this string?
String format is : d MMM yyyy
code:
mAlertDate is 25 Apr 2016
alertDateTest is 25 Apr 2016
public void updateNotification() {
try {
String alertDateTest = i.getStringExtra("taskAlertDate");
String alertTimeTest = i.getStringExtra("taskAlertTime");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(alertDateTest);
if (alertDateTest.equals(mAlertDate)) {
String formattedString = date.toString();
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, mHourOfTheDay);
c.set(Calendar.MINUTE, mMinute);
c.set(Calendar.SECOND, 0);
mYear = new DateTime(formattedString).getYear();
mMonth = new DateTime(formattedString).getMonthOfYear();
mDayOfMonth = new DateTime(formattedString).getDayOfMonth();
c.set(Calendar.YEAR, mYear);
c.set(Calendar.MONTH, mMonth);
c.set(Calendar.DAY_OF_MONTH, mDayOfMonth);
date = c.getTime();
Log.d("AlertDate", String.valueOf(date));
Toast.makeText(AddTaskActivity.this, String.valueOf(date), Toast.LENGTH_SHORT).show();
intent = new Intent(AddTaskActivity.this, NotificationReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getBaseContext(), _mId, intent, 0);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
} catch (ParseException e) {
}
}
EDIT:
I have a function onDateSet of date picker in this I have a string format and the string is formatted to the same format:
#Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
// String date = "You picked the following date: "+dayOfMonth+"/"+(++monthOfYear)+"/"+year;
SimpleDateFormat df = new SimpleDateFormat("d MMM yyyy");
mCalendar = Calendar.getInstance();
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, monthOfYear);
mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
Date date = mCalendar.getTime();
if(DATE_PICKER_TAG == 1)
{
mYear = year;
mMonth = monthOfYear;
mDayOfMonth = dayOfMonth;
mAlertDate = df.format(date);
alertDate.setText(String.valueOf(mAlertDate));
}
if(DATE_PICKER_TAG == 3) {
mDueDate = df.format(date);
dueDate.setText(String.valueOf(mDueDate));
}
}
EDIT: Tried by your answer Umesh Saraswat
String alertDateTest = i.getStringExtra("taskAlertDate");
String alertTimeTest = i.getStringExtra("taskAlertTime");
SimpleDateFormat simpleDateFormate = new SimpleDateFormat("dd MMM yyyy");
Date date = simpleDateFormate.parse(alertDateTest);
// Date date = df.parse(alertDateTest);
if (alertDateTest.equals(mAlertDate)) {
String formattedString = date.toString();
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, mHourOfTheDay);
c.set(Calendar.MINUTE, mMinute);
c.set(Calendar.SECOND, 0);
c.setTime(date);
c.set(Calendar.YEAR, mYear);
c.set(Calendar.MONTH, mMonth);
c.set(Calendar.DAY_OF_MONTH, mDayOfMonth);
date = c.getTime();
The date i got is wed dec 31. String was 26 april 2016. Time did not get set.
Thank you..
To parse Date String with 25 Apr 2016 use dd MMM yyyy string format.
For more information visit, SimpleDateFormat
You can use either "dd MMM yyyy" or "d MMM yyyy"
as I have understood your question,
have a string in date format which I want to parse in date object.
For this first tou need to parse the String date into date formate
SimpleDateFormate simpleDateFormate = new SimpleDateFormate("dd MMM yyyy");
Date date = simpleDateFormate.parse("Your date");
I want to set the time as string in calendar object and want to set an
alert at the same time.
Calendar c = Calendar.getInstance();
calendar.setTime(date)
c.set(Calendar.HOUR_OF_DAY, mHourOfTheDay);
c.set(Calendar.MINUTE, mMinute);
c.set(Calendar.SECOND, 0);
;
If you want to use Calendar object date into a String formate then
SimpleDateFormate simpleDateFormate = new SimpleDateFormate("Your Date format String");
String date = simpleDateFormat.format(c.getTime);
Just added Locale to the format and it worked..
SimpleDateFormat simpleDateFormate = new SimpleDateFormat("dd MMM yyyy",Locale.ENGLISH);
Thank you all..

How convert timestamp to date on android?

I want convert timestamp to date and set time in this day to 12.00.
How can I do this?
If I use this:
Date date = new Date(event.getActionDate()*1000);
I can't set hour or minutes to this date, becouse methods for this operations are deprecated.
Explanation:
I have timestamp -
1461924872 // Fri, 29 Apr 2016 10:14:32 GMT
I want change hour in this timestamp (10:14 to 00.00).
long timeInMillis = 1461877200000l;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeInMillis);
System.out.println("Date1:"+calendar.getTime().toString());
calendar.set(Calendar.HOUR_OF_DAY, 0); //For 12 AM use 0 and for 12 PM use 12.
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date date = calendar.getTime();
System.out.println("Date2:"+date.toString());
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();//get your local time zone.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
sdf.setTimeZone(tz);//set time zone.
String localTime = sdf.format(new Date(time) * 1000));
Date date = new Date();
date = sdf.parse(localTime);
private String getDate(String timeStampStr){
try{
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date netDate = (new Date(Long.parseLong(timeStampStr)));
return sdf.format(netDate);
}
catch(Exception ex){
return dateInStr;
}
}
let me know if it is working?

Getting next Sunday's date

I'm using the following code to get the date of the upcoming Sunday. When my device language is English, it is working properly. But it is not when device language is changed to Spanish.
Eg : for English it gives 2015-11-22 but for Spanish it gives 2015-11-29.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getDefault());
calendar.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
calendar.clear(Calendar.MINUTE);
calendar.clear(Calendar.SECOND);
calendar.clear(Calendar.MILLISECOND);
// get start of this week in milliseconds
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
calendar.add(Calendar.DAY_OF_WEEK, 7);
Date currentTime = calendar.getTime();
date = dateFormat.format(currentTime);
Change declaration of your SimpleDateFormat to this:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd" , Locale.US);
It works for every language.
Actually, you get, set wrong day:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getDefault());
// get start of this week in milliseconds
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK, calendar.SUNDAY);
Date currentTime = calendar.getTime();
String date = dateFormat.format(currentTime);

Current date should add 12 hours more in android

In my application i want to add 12 hours with the current date and time,then i have to show it in this format "yyyy-MM-dd HH:mm:ss". I wrote the code but unable to add 12 hours. How can i do? please help.
My code is :
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);
The Calendar class has the add method which you can use to add certain units.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 12);
Date date = cal.getTime();
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);
How about
Calendar cal = Calendar.getInstance();
Date date = cal.getTime()+12*60*60*1000;
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);
You should add the 12 hours to the Calendar object like this:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 12)
Date date = cal.getTime();
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);

Categories

Resources