Android - cannot correctly parse the date of tomorrow - android

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

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.

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/

Convert a date and time string according to UTC format

I am having a date string 2012-11-21 13:11:25 which I get from local database. I have to convert this according to UTC settings and display it on a particular screen. So if its GMT+05:30 it should be displayed as 2012-11-21 18:41:25 on the screen. How can I do this conversion. I have checked some of the questions but that didn't work out.
I am able to get a Date object that returns something like Wed Nov 21 13:11:25 GMT+05:30 2012 after this I need to get the time as 18:41:25 and date as 11-21-2012
Thanks in advance
Your df and inputFmt must use the same format.
But I think you should do it like this:
Date myDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
calendar.setTime(myDate);
Date time = calendar.getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("MMM dd, yyy h:mm a zz");
String dateAsString = outputFmt.format(time);
System.out.println(dateAsString);
Get UTC from current time :
public String getCurrentUTC(){
Date time = Calendar.getInstance().getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outputFmt.setTimeZone(TimeZone.getTimeZone("UTC"));
return outputFmt.format(time);
}
Best way to get formatted string of Date in required format is
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String formatted = dateFormat.format(date);
//This is my input date
String dtStart = "2019-04-24 01:22 PM";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
Date date = null;
try {
date = format.parse(dtStart);
getDateInUTC(date)
} catch (ParseException e) {
e.printStackTrace();
}
//This Method use for convert Date into some UTC format
public static String getDateInUTC(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateAsString = sdf.format(date);
System.out.println("UTC" + dateAsString);
return dateAsString;
}

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

how to convert date format in android

I am getting date into string in YYYY/MM/DD HH:MM:SS format.I want to change it into the mm/dd/yyyy HH:mm:ss and also it will show AM and PM how can I do this.please help me
Thank you
To get AM PM and 12 hour date format use hh:mm:ss a as string formatter WHERE hh is for 12 hour format and a is for AM PM format.
Note: HH is for 24 hour and hh is for 12 hour date format
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
Example
String date = "2011/11/12 16:05:06";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
You can use the SimpleDateFormat for the same kinds of any date operations.
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
SimpleDateFormat DesiredFormat = new SimpleDateFormat("MM/dd/yyyy HH:MM:SS a");
// 'a' for AM/PM
Date date = sourceFormat.parse("2012/12/31 03:20:20");
String formattedDate = DesiredFormat.format(date.getTime());
// Now formattedDate have current date/time
Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();
just use the Time class. Try something similar to this.
Time time = new Time();
time.set(Long.valueOf(yourTimeString));
If you really need a Date object just try this.
Date date = new Date(Long.parse(yourTimeString));
Use android.text.format.Time to the conversion. You can pass the time as text and it will return you the time in desired format by using timeInstance.format("");
you need to provide formatter.
Refer to following:
Time : http://developer.android.com/reference/android/text/format/Time.html
Formatter: http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html
you can use any combination of formatter string to work with it :)
you can use SimpleDateFormat or DateFormat for this here is the example
SimpleDateFormat gsdf = new SimpleDateFormat("YYYY/MM/DD HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
try{
Date d = gsdf.parse("2011/12/13 16:17:00");
System.out.println("new date format " + sdf.format(d));
}catch(Exception){
// handle exception if parse time or another by cause
}
public static String getFormatedDate(String strDate, String sourceFormate,
String destinyFormate) {
SimpleDateFormat df;
df = new SimpleDateFormat(sourceFormate);
Date date = null;
try {
date = df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
df = new SimpleDateFormat(destinyFormate);
return df.format(date);
}
and use like
getFormatedDate(strDate,
"yyyy-MM-dd HH:mm:ss", "mm/dd/yyyy")

Categories

Resources