This question already has answers here:
How to convert Gregorian string to Gregorian Calendar?
(3 answers)
Closed 4 years ago.
I converted Gregorian Calendar to String by using toString() method to upload my DataBase. ( Because My Database doesn't apply Calendar type )
Here Example.
String Time =
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=java.util.SimpleTimeZone[id=GMT,offset=0,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=2,WEEK_OF_YEAR=12,WEEK_OF_MONTH=4,DAY_OF_MONTH=22,DAY_OF_YEAR=81,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]
How do I parse it to Calendar type?
Much appreciate if you guys could help.
Use SimpleDateFormat like this.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH,23); (etc)
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = simpleDateFormat.format(calendar.getTime());
Well, you can save GregorianCalendar's millis in the database which will be of a long type. Here is how you do it:
long millis = GregorianCalendar.getInstance().getTimeInMillis();
and when you want the GregorianCalendar instance in your app just get the millis from the database and add those millis to GregorianCalendar instance like this :
GregorianCalendar gregorianCalendar = (GregorianCalendar) GregorianCalendar.getInstance();
gregorianCalendar.setTimeInMillis(millis);
Don't save the string representation of Calendar. Save the timestamp as Long.
Here an example:
Calendar cal1 = GregorianCalendar.getInstance();
long timeStamp = cal1.getTimeInMillis();
Calendar cal2 = GregorianCalendar.getInstance();
cal2.setTimeInMillis(timeStamp);
Related
I have retrieved a Date from a SQLiteDatabase and have formatted it to how I want via the following;
String steepingDate = (c.getString(3));
SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy");
Date steepingdate = formatter.parse(steepingDate);
I now want to give the user the option to increase whatever date is in steepingdate by a certain amount of days that the user can input
I know you can use;
Date today = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 10);
For example to add 10 days onto todays date
But how do you do it so that it uses steepingdate instead of todays date
Thanks
UPDATE;
The calendar is working as I want, but I now want to save the new data to the database, the full code is as following;
String steepingDate = (c.getString(3));
SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy");
Date steepingdate = formatter.parse(steepingDate);
Integer amountDays = Integer.parseInt(TSExtend.getText().toString());
Calendar ca = Calendar.getInstance();
ca.setTime(steepingdate);
ca.add(Calendar.DAY_OF_YEAR, amountDays);
DateFormat dateFormat = new SimpleDateFormat("dd/MMM/yyyy");
String newDate = dateFormat.format(ca);
I'm getting the error;
Bad class: class
java.util.GregorianCalendar
Any ideas?
To add 10 days to steepingdate, you can use:
Calendar calendar = Calendar.getInstance();
calendar.setTime(steepingdate);
calendar.add(Calendar.DAY_OF_YEAR, 10);
it the number is provided, through the user interface, you can use the View.OnClickListener and when onClick is fired, read the value from an EditText, and use this value instead of 10
Set the time of the calendar to your date, then add the days
Calendar cal = Calendar.getInstance();
cal.setTime(steepingdate);
cal.add(Calendar.DATE, 10);
UPDATE:
You can't directly format a Calendar, first get the Date from the Calendar, then format it.
String newDate = dateFormat.format(ca.getTime());
This question already has an answer here:
How to get day, month, year and hour, minutes, second from DateTime format?
(1 answer)
Closed 8 years ago.
I have a field in mysql database of type DateTime.The value in this field is sent as a string to my android app( say in the form "2014-11-21 06:00:00") .I need to obtain year,month,day,hours and minutes value from the string and set it to a calendar instance.Please help me.
String s = "2014-11-21 06:00:00";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(simpleDateFormat.parse(s));
System.out.println(calendar.getTime());
To convert your String in a Date object you can use a SimpleDateFormat:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.UK);
Date date = simpleDateFormat.parse("2014-11-21 06:00:00");
Then you can use a Calendar with the Date object you got before
Calendar c = Calendar.geInstance();
c.setTime(date);
and use c.get(Calendar.SECOND) to get the seconds, for instance
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();
This question already has answers here:
Display the current time and date in an Android application
(23 answers)
Closed 9 years ago.
I need to format a date and time as Nov 15 2012 18:55 GMT.
Time should be in GMT and 24hr format.I have tried using the following code but it gives time in 12hr format.Is there any way?
DateFormat currentDateTimeString = DateFormat.getDateTimeInstance();
currentDateTimeString.setTimeZone(TimeZone.getTimeZone("gmt"));
gmtTime = currentDateTimeString.format(new Date());
You can use DateFormats to convert Dates to Strings in any timezone:
DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("gmt"));
String gmtTime = df.format(new Date());
OR
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("dd-MM-yyy HH:mm:ss z");
date.setTimeZone(TimeZone.getTimeZone("GMT"));
String localTime = date.format(currentLocalTime);
System.out.println(localTime);
I am doing a programme that stores the present time and date in "yyyy-MM-dd'T'HH:mm:ss.SSSZ" this format. and I am storing it in database as a string. when i am collecting the data i need the individual values like day, year, min, seconds etc.. how can i do this?
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String now = formatter.format(new Date());
Thank you,
Just use parse instead of format :
String dateFromDB = "";
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date yourDate = parser.parse(dateFromDB);
And then you can can read any field you want using java.util.Date & Calendar API :
Calendar calendar = Calendar.getInstance();
calendar.setTime(yourDate);
calendar.get(Calendar.DAY_OF_MONTH); //Day of the month :)
calendar.get(Calendar.SECOND); //number of seconds
//and so on
I hope it fits your needs
I'm suggesting that you store times in the DB as "timeInMillis". In my experience it simplifies code and it allows you to compare times values to eachother.
To store a time:
Calendar calendar = Calendar.getInstance(); // current time
long timeInMillis = calendar.getTimeInMillis();
mDb.saveTime (timeInMillis); // adjust this to work with your DB
To retrieve a time:
long timeInMillis = mDb.getTime();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis (timeInMillis);
int milliSeconds = calendar.get(MILLISECOND);
//etc
There are these methods available to get the individual parts of a date
getDate()
getMinutes()
getHours()
getSeconds()
getMonth()
getTime()
getTimezoneOffset()
getYear()
Try using : int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
Here,
Calendar.HOUR_OF_DAY gives you the 24-hour time.
Calendar.HOUR gives you the 12-hour time.