private static final Date myDate =? - android

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/

Related

How to return current date in format "YYYY-mm-dd" as a Date datatype?

I know there are many ways to solve this but I am looking for a simple one line solution.
Typically it would be
new SimpleDateFormat("yyyy-MM-dd").format(new Date())
However this returns a string and I need an object of the Date type.
Using the GregorianCalendar class is okay too since it can return a Date type.
thanks
Try it out in this way:
String strDate = sdf.format(cal.getTime())
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf1 = new SimpleDateFormat();
sdf1.applyPattern("yyyy-MM-dd");
Date date = sdf1.parse(strDate);
Date dateObj = new SimpleDateFormat("yyyy-MM-dd").parse( new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
Date d = (new SimpleDateFormat("yyyy-MM-dd")).parse("2011-12-13");
or
Date now = new Date();

Android - cannot correctly parse the date of tomorrow

I'm trying to get the date of tomorrow and insert it into a Date type variable, but for some reason I'm getting an empty string.
Here his the code:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
String time_Date_str = cal.getTime().toString();
try
{
Date myDate = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(time_Date_str);
Toast.makeText(this, "time_Date_str", Toast.LENGTH_SHORT).show();
t.setDueDate(myDate);
t.setHasDate(true);
} catch(Exception e){myDate=null;}
Please let me know what I'm doing wrong.
You're not using your sdf variable to format the date... try this:
String time_Date_str = sdf.format(cal.getTime());

android date picker set time

i would like to set the date format of my date picker like this:
yyyy-MM-dd HH:ss:mm
I hope this is the correct format for dates in databases. if not, please tell me which format is right.
also i would like to set the hour,minute and second of selected date like 0
Example:
2015-11-30 00:00:00
For this i use this code:
long dateTime = DatePicker.getCalendarView().getDate();
Date date = new Date(dateTime);
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
Android Studio tells me, that setHours,setMinutes and setSeconds is deprecated. what is the new way to set this values?
Use Calendar Object instead of Date:
Calendar c = Calendar.getInstance();
c.setTimeInMillis(DatePicker.getCalendarView().getDate(););
c.set(Calendar.HOUR,00);
like wise
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
String dateString = "2015-11-30 00:00:00";
try {
Date date = df.parse(dateString);
df.applyPattern("yyyy-mm-dd hh:mm:ss");
String result = df.format(date);
}
catch (ParseException e){
e.printStackTrace();
}

Date retreival in 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());

Convert DateString in a particular format

I have a problem that I have Date String in the format "2011-09-28T10:33:53.694+0000". I want to change this Date format in MM/DD/YYYY, don't know How? Please suggest me for right result.
Something like this. Check the details for your "from" format.
DateFormat from = new SimpleDateFormat("yyyy-MM-ddThh:mm:ss"); // not sure about the details
from.setLenient(false);
DateFormat to = new SimpleDateFormat("MM/dd/yyyy");
to.setLenient(false);
Date d = from.parse(dateStr);
System.out.println(to.format(d));
String string = "2011-09-28T10:33:53.694+0000";
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);
SimpleDateFormat outFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date date;
String output;
try {
date = inFormat.parse(string);
output = outFormat.format(date); // 28/09/2011
} catch (ParseException e) {
e.printStackTrace();
}
Do it this way ---->
Create object Date class
Date date = new Date();
Create object of Simple Date Format class
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Note -> MM - will give month in character and dd and yyyy will give date and year in numeric
Convert date into string
String s = sdf.format(date);
and you are done with it...

Categories

Resources