I am getting date time as String in this format from the server:2017-11-15 16:27:02
I want to set Calendar with this time so I Do something like this:
String[] dateTime = response.body().getUser().getUserSessionExpire().split(" ");
String[] date = dateTime[0].split("-");
String[] time = dateTime[1].split(":");
Log.i("TIMMEE", "Received TIME: " + response.body().getUser().getUserSessionExpire());
int month = Integer.parseInt(date[1]);
int day = Integer.parseInt(date[2]);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, Integer.parseInt(date[0]));
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR, Integer.parseInt(time[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(time[1]));
calendar.set(Calendar.SECOND, Integer.parseInt(time[2]));
For example, if I get
2017-11-15 16:27:02
Calendar will set for :
2017-12-15 16:27:02 Or 2017-11-16 16:27:02
Actually, Month or Day will change!
What is the problem?
Thankyou for your answers.
You do not need to split the date . SimpleDateFormat is responsible for formating . So you can parse the date and get Date object from it . See code below and read the SimpleDateFormat(linked above).
Calendar calendar=Calendar.getInstance();
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd H:m:s", Locale.getDefault());
try {
Date d = DATE_FORMAT.parse("2017-11-15 16:27:02");
calendar.setTime(d);// Use this calender
} catch (ParseException e) {
e.printStackTrace();
}
Related
I want to add selected date from data that i get from table. here is my array example
{"data":[{"Tanggal":"2019-07-02","Flag":1},{"Tanggal":"2019-07-03","Flag":0}]}
for real i don't know how to do it, my teacher is google, but i think i miss something. so i'm trying to write it manual like this
String date = "2019-07-02";
String parts[] = date.split("-");
int day = Integer.parseInt(parts[2]);
int month = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[0]);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
long milliTime = calendar.getTimeInMillis();
widget.setDateSelected(calendar,true);
but i get an error
widget.setDateSelected(calendar,true); // wrong 1st argument type
How can i achieve it , thanks in advance.
Btw, i using this https://github.com/prolificinteractive/material-calendarview
Use another method setSelectedDate(LocalDate date) from this library to set the date.
Here is the code...
try{
//Option 1
String myDate = "2019-07-02";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
Date date = dateFormat.parse(myDate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
LocalDate ld = LocalDate.of(cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH) + 1,
cal.get(Calendar.DAY_OF_MONTH));
//Option 2
LocalDate ld1 = LocalDate.of(2019,07,02);
//and finally pass it in parameter
widget.setSelectedDate(ld);//you can also pass ld1
}catch (ParseException e){
e.printStackTrace();
}
And use below import for LocalDate
import org.threeten.bp.LocalDate;
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..
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);
In my application i have 1 edittext box,in this user will enter some date.What i want is i have to get the date of 7th day from the user entered date.I searched in google,i found 1 solution.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
cal.add("field", +7);
String currentDateandTime = sdf.format(cal.getTime());
In the above cal.add("field",+7)-->Field is int.But my date format is string.So i cant use here..Please help me..
Get date from SimpleDateFormat and add this date object to calender then change into calender. And again get new Updated date from Calender. Wait i will post code
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date UserEnterDate = sdf.parse("String from your editbox");
Calendar calendar = Calendar.getInstance();
calendar.setTime(UserEnterDate);
int day = calendar.get(Calendar.DAY_OF_MONTH);
day = day + 7;
calendar.set(Calendar.DAY_OF_MONTH, day);
String newDate = calendar.get(Calendar.DAY_OF_MONTH) + "/"
+ calendar.get(Calendar.MONTH) + "/"
+ calendar.get(Calendar.YEAR);
} catch (Exception e) {
// TODO: handle exception
}
As you said thatyou got the date in string format.
So let me start from there
Suppose the date is:
String dt = "2008-01-05"; // Start date
Then do thhis::
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse(dt));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.add(Calendar.DATE, 7); // number of days to add
dt = sdf.format(c.getTime());
System.out.println(""+dt);
Hope.this will definitely help you.
Enjoy!!!
I suggest better you use DatePickerDialog in onClick() of EditText
then you will get individual Date Month Year. Then you can set your date
Date+=7
you can get Date Object from a String if you know the format, by your question the format is:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
cal.add("field", +7);
String currentDateandTime = sdf.format(cal.getTime());
now use this date format to parse a string to get Date object, say:
Date dt=sdf.parse(txtDt.getText().toString());
now Set this date to Calendar Object:
Calendar cal=Calendar.getInstance();
cal.setTime(dt);
now you need to add 7 days to this date, so do as:
cal.add(Calendar.DAY_OF_MONTH, 7);
now you have been added 7 days to the date successfully, now get Date from this Calendar object, by using:
Date dtNew=cal.getTime();
and you can convert it to readable string using:
String strNewDt=sdf.format(dtNew);
You have to write the name of the field you want to modify to the "field" parameter, here#s a link to the calendar reference. The calendar object is not a string, but a whole different creature that youre using. Use its functions to do it.
http://developer.android.com/reference/java/util/Calendar.html
And so you would write
!edit, you actually have to set the calendar using ints and parse your string to match. use the set function from the calendar:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = new Calendar;
cal.set(int year, int month, int day, int hourOfDay, int minute)
cal.add(DATE, 7);
String currentDateandTime = sdf.format(cal.getTime());
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);