Date retreival in android - android

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());

Related

Could not print time after conversion from string format

// For Date validation
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String datechosen = dateText.getText().toString() ;
Date dateselected = simpleDateFormat1.parse(datechosen);
System.out.println(dateselected);
// For Time validation
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String timechosen = timeText.getText().toString();
Date timeselected = simpleDateFormat.parse(timechosen);
Calendar cal = Calendar.getInstance();
cal.setTime(timeselected);
cal.add(Calendar.HOUR, noofhourselected);
cal.add(Calendar.MINUTE, noofminselected);
timeselected = cal.getTime();
System.out.println(timeselected);
I am working on converting the string which i have into the date and time format. For example, the string datechosen contain "26/10/2020". I am able to to convert it into date format and print it out.
But for the time string, i am unable to print them out. I am facing the error below:
Screenshot of the log message
But if i swap the position of the codes the other way round,
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String timechosen = timeText.getText().toString();
Date timeselected = simpleDateFormat.parse(timechosen);
Calendar cal = Calendar.getInstance();
cal.setTime(timeselected);
cal.add(Calendar.HOUR, noofhourselected);
cal.add(Calendar.MINUTE, noofminselected);
timeselected = cal.getTime();
System.out.println(timeselected);
// For Date validation
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String datechosen = dateText.getText().toString() ;
Date dateselected = simpleDateFormat1.parse(datechosen);
System.out.println(dateselected);
The time will be printed instead
These are the two input fields
You are setting date in wrong format for time. for cal.setTime(timeselected); setTime takes Date Refer java doc
Use same format as used for Date.

How to set selecteddate in material-calendarview

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;

How to fetch recent seven days from specific date

If the date is 2017-03-30 that i want to fetch the date from 2017-03-23 to 2017-03-30
I try to use this code let my String change to Date format
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dateParse = sdf.parse("2017-03-30");
then i'm stuck , cause i take the reference is get the current time like this
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -7);
//may be my dateParse should put here , but i don't know how to do
Date monday = c.getTime();//it get the current time
String preMonday = sdf.format(monday);
Is any one can teach me how to fetch these seven days ? Thanks in advance.
You can use the code below
SimpleDateFormatdateFormat = new SimpleDateFormat("dd MMM yyyy");
Calendar c = Calendar.getInstance();
String date = dateFormat.format(c.getTime());
c.add(Calendar.DATE, 7);
String date1 = dateFormat.format(c.getTime());
Parse the date:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = sdf.parse("2017-03-30");
First Solution 1) And then either figure out how many milliseconds you need to subtract:
Date newDate = new Date(myDate.getTime() - 604800000L); // 7 * 24 * 60 * 60 * 1000
Second Solution 2) Or use the API provided by the java.util.Calendar class:
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -7);
Date newDate = calendar.getTime();
Then, if you need to, convert it back to a String:
String date = dateFormat.format(newDate);
This answer is from here
EDIT:
If you need output as 2017-03-29 2017-03-28 2017-03-27 ...... 2017-03-23 then try below code
for(int i = 1; i <= 7; i++){
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -i);
Date newDate = calendar.getTime();
String date = dateFormat.format(newDate);
//here in date you can get all date from and output as 2017-03-29 2017-03-28 2017-03-27 ...... 2017-03-23
}
Hope you need this

private static final Date myDate =?

I was having difficult to put correct types for:
private static final Date myDate =
I tried to put "mm-dd-yyyy" but it said it cannot be a string.
I tried 'mm-dd-yyyy' but it said too many characters in character literal.
I dont know what is other types I should put. Help please.
If you want to have a Date with a specific time you can use a Calendar object.
If you want to have todays Date you can just use:
private static final Date myDate = new Date();
Specific date:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2015);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date myDate = cal.getTime();
That would be 01.01.2015
Try with this:
SimpleDateFormat f = new SimpleDateFormat("mm-dd-yyyy");
Date date = f.parse(yourString);
I expect it will works for you!
EDIT: It gives to you an error because you are trying set to your variable myDate a String, and it's a type of Date.
Try something like:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "7-Jun-2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
...as you can see here:
http://www.mkyong.com/java/how-to-convert-string-to-date-java/

Date object from simple string - Java and Android [duplicate]

This question already has answers here:
How to get the day, year, hours, min Individually from date format "yyyy-MM-dd'T'HH:mm:ss.SSSZ"?
(4 answers)
Closed 9 years ago.
specific date is "2013-11-12".
I want to extract day, month and year from above date. Please let me know how can I extract?
You can use split().
Example :
String mydate="2013-11-12"; //year-month-day
String myyear=mydate.split("-")[0]; //0th index = 2013
String mymonth=mydate.split("-")[1]; //1st index = 11
String myday=mydate.split("-")[2]; //2nd index = 12
you can use substring method to extract the particular characters from the above string like:
String year=date.substring(0,4); //this will return 2013
String month=date.substring(5,7); //this will return 11
String day=date.substring(8,10); //this will return 12
You can also use Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse("2013-11-12"));
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
You can do it using SimpleDateFormat and Calendar Class.
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
cal.setTime(sdf.parse("Mon Mar 14 16:02:37 GMT 2011"));// all done
Now you can use this cal Object to do whatever you want. Not just the date, month or year. you cam use it to perform various operations, such as add month, add year etc...
Create date object from your date string first, like...
String yourDateString = "2013-11-12";
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd");
Date yourDate = parser.parse(yourDateString);
Now create Calender instance to get further information about date...
Calendar calendar = Calendar.getInstance();
calendar.setTime(yourDate);
int months = calendar.get(Calendar.DAY_OF_MONTH);
int seconds = calendar.get(Calendar.SECOND);
// and similarly use calender.getXXX
Hope this helps...
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date testDate = null;
try {
testDate = sdf.parse("2013-11-12");
}
catch(Exception ex) {
ex.printStackTrace();
}
int date= testDate.getDate();
int month = testDate.getMonth();
int year = testDate.getYear();

Categories

Resources