Using Android Calendar to get the current time - java

I'm trying to get the current time (HH:MM:SEC:MILLISEC) in an android app. I'm using this piece of code:
Calendar c = Calendar.getInstance();
int time_start = c.get(Calendar.MILLISECOND);
"Field number for get and set indicating the minute within the hour. E.g., at 10:04:15.250 PM the MILLI is 250."
I've looked through the other methods, but I couldn't find anything specifying it would output everything. Is there anyway I can get H:M:Sec:MilliSec? or do I just have to do something like
c.get(Calendar.HOUR).get(Calendar.MINUTE).get(Calendar.SECOND).get(Calendar.MILLISECOND).

You could try with SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
Like this perhaps:
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
String test = sdf.format(cal.getTime());
Log.e("TEST", test);

What about this?
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:S");
String result = sdf.format(Calendar.getInstance().getTime());
System.out.println(result);

I would go the System.currentTimeMillis() or the new Date() way, and put these in a SimpleDateFormat, to get exactly the output you like

Related

How to just change the TimeZone with SimpleDateFormat

I want to convert date from this format to Tue Sep 08 14:27:00 IST 2015 to 2015-09-08T14:27:00-0500 how to do this with SimpleDateFormat?
I tried like this way
Calendar cl = Calendar.getInstance();
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
ft.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
ft.format(cl.getTime());
the above code giving output 2015-09-08T04:57:00-0400 but I want to change only timezone and it should be -0500
How could I do this?
SimpleDateFormat uses a Calendar instance to work with Dates. If you change the timezone, the timezone of this Calendar instance is changed. If you really want to change only the output - in your case the timezone, without changing the Time (i do not know why you want to do this, but i assume you have a reason), you could do something like this:
Calendar cl = Calendar.getInstance();
TimeZone tz = TimeZone.getTimeZone("US/Eastern");//TimeZone.getDefault();
int offset = tz.getRawOffset();
String offsetStr = ((offset < 0) ? "-" : "+") + String.format("%02d%02d",
Math.abs(offset / 3600000), (offset / 60000) % 60);
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'" + offsetStr+"'");
String dt = ft.format(cl.getTime());
use:
ft.setTimeZone(TimeZone.getTimeZone("GMT-5"))
sample:
Calendar cl = Calendar.getInstance();
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
ft.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
System.out.println(ft.format(cl.getTime()));
ft.setTimeZone(TimeZone.getTimeZone("GMT-5"));
System.out.println(ft.format(cl.getTime()));
produces:
2016-03-23T16:39:22-0400
2016-03-23T15:39:22-0500
source: https://docs.oracle.com/javase/6/docs/api/java/util/TimeZone.html
Below code is working fine for me
Calendar cl = Calendar.getInstance();
ZoneOffset offset = ZoneOffset.of(ZoneId.SHORT_IDS.get("EST"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'"+offset.getId()+"'");
formatter.format(cl.getTime());

android getDate from milliseconds stored in String field

I have a date stored in a String field in SQLITE with the String value
"/Date(1411472160000+0100)/"
how can I convert this back into a date format , the code below doesn't work. I think I need to convert from the milliseconds first but I cant see how to even get the above text into a long format first ?
any suggestions ?
Date convertedDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm",
java.util.Locale.getDefault());
convertedDate = dateFormat.parse(dateString);
return dateFormat.format(convertedDate);
Well, a substring from the indexOf("(") to the indexOf("+") and you should find the date in milli.
From there, I believe you can find the date ;)
String s = "/Date(1411472160000+0100)/";
s = s.substring(s.indexOf("(") + 1, s.indexOf("+"));
Date d = new Date(Long.parseLong(s));
With the same structure, you can find the timezone (+0100) (from "+" to ")") and work with a Calendar to find the right time for the right time area.
First you have to parse out the time value from String i.e. "1411472160000+0100" part.
Here in "1411472160000+0100" , "+0100" is the timezone info. If you don't want to consider the timezone, then you can take following approach.
Approach-1
long timestamp = 1245613885;
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getDefault());
calendar.setTimeInMillis(timestamp * 1000);
int year = calendar.get(Calendar.YEAR);
int day = calendar.get(Calendar.DATE);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
then to get the date in your specified format you can use-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = sdf.format(calendar.getTime());
System.out.println(dateString); // 2009-06-21 15:51:25
Besides this approach, there is an excellent Java Date library called JodaTime.
If you want to incorporate the timezone info , you can refer to this constructor from JodaTime.
http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html#DateTime-long-org.joda.time.DateTimeZone-

Calender issue with time zone

I'm having a weird situation with Java Calendar. I'm using dozer mapper to map the objects.
I want to write a method that will convert this object to the following format. yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
say element 2010-11-11T09:30:47.000Z
public Calender getValue(Date source,Calender c) {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.setTime(source);
return calendar;
}
When I run the program, it is printing
2010-11-11T04:00:47.000Z - Because we are setting the Timezone to be GMT, (9.30 - 5.30 = 4.00)
I want my object to have same format and value.if I don't set TimeZone to GMT, it will show as 2008-11-21T09:30:47.000+05:30.
I want it as 2010-11-11T09:30:47.000Z.
I tried added 5.30 to calender.
calendar.add(Calendar.HOUR, 5);
calendar.add(Calendar.MINUTE, 30)
then it works.But if this is ran from any other place, difference won't be 5.30.So I cannot add 5.30 to calenderget
Is there any way to get rid of this problem? I want to return Calender object.
Any suggestions or help would be much appreciated
Use a pattern. F.E:
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
Also,
SimpleDateFormat dateformatyyyyMMdd = new SimpleDateFormat("yyyyMMdd");
String date_to_string = dateformatyyyyMMdd.format(dateNow);
you can use SimpleDateFormat like this.
SimpleDateFormat formatter, FORMATTER;
formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String oldDate = "2011-03-10T11:54:30.207Z";
Date date = formatter.parse(oldDate.substring(0, 24));
FORMATTER = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS");
System.out.println("OldDate-->"+oldDate);
System.out.println("NewDate-->"+FORMATTER.format(date));
Output OldDate-->2011-03-10T11:54:30.207Z NewDate-->10-Mar-2011 11:54:30.207

How to format time including milliseconds

I'm trying to get a time string in the format of YYYYMMDD-HHMMSSMilliseconds in Android
Ex: 20130312-1723437520 (2013 March 12th, 17 Hour 23 Minutes 43 Seconds 7520 Milliseconds)
Time now = new Time();
now.setToNow();
String snapshotTime = now.format("yyyyMMdd-HHmmss");
First of all, above code doesn't even work properly. snapshotTime is always set to the format string itself.
Second of all, according to the Android documentation, there's no way to record milliseconds.
How can I accomplish this?
See the SimpleDateFormat class, you can format a Date object into the required format (upper-case S will give millis)
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date now = new Date();
String str = fmt.format(now);
That said, using Joda Time is usually a good idea (Proguard will strip code you don't use).
You'll have to use the strftime formatting, as noted in the Android docs.
Time now = new Time();
now.setToNow();
String snapshotTime = now.format("%Y%m%d-%H%M%S");
If you really want to use milliseconds than I would recommend SimpleDateFormat.
Try getting the time as unix timestamp with milliseconds from
long currentTime = System.currentTimeMillis();
or convert your time to milliseconds:
long currentTime = now.toMillis(true);
Then you can convert this to your desired date:
Time now = new Time();
now.set(currentTime);
String snapshotTime = now.format("%Y%m%d-%H%M%S")+""+(currentTime%1000);
Didn't test it but hope it works :)
I would recommend to use this little library, it's very helpful when working with dates. Have a look at the DateTimeFormatter class.
As an alternative use Calendar and SimpleDateFormater (you'll have to adjust the format string of course, see this for explanation of the symbols)
Calendar c = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy-SSSS");
String date = sdf.format(c.getTime());
You can try this:
public static String format() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd-HHmmssSSS");
Date now = new Date();
return simpleDateFormat.format(now);
}

How to substract two times written as hh:mm and display the result in a textview

I'm a beginner in java/android but I am trying to make an app. A user must type in a time like 13:45 and then the app should substrat ex. 00:30 minutes from that time. That gives the result 13:15.
I have tried many different things but it wont work.
I have somthing like this. (At this stage I've hardcoded two times)
String time1 = "00:30";
String time2 = "13:45";
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
Date date1 = format.parse(time1);
Date date2 = format.parse(time2);
long result= date2.getTime() - date1.getTime();
String strLong = Long.toString(result);
textView4.setText(strLong);
I'm getting an error in the format.parse(time1) and format.parse(time2). Is this the right way to do this? Any help? Thanks
Use Calendar type.
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
Date date2 = format.parse(time2);
Calendar cal=Calendar.getInstance();
cal.setTime(date2);
cal.add(Calendar.MINUTE, -30); //Subtract 30 Min
String strLong= format.format(cal.getTime());
textView4.setText(strLong);

Categories

Resources