How to convert HH:mm:ss.SSS to milliseconds? - java

I have a String 00:01:30.500 which is equivalent to 90500 milliseconds. I tried using SimpleDateFormat which give milliseconds including current date. I just need that String representation to milliseconds. Do I have to write custom method, which will split and calculate milliseconds? or Is there any other way to do this? Thanks.
I have tried as follows:
String startAfter = "00:01:30.555";
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
Date date = dateFormat.parse(startAfter);
System.out.println(date.getTime());

You can use SimpleDateFormat to do it. You just have to know 2 things.
All dates are internally represented in UTC
.getTime() returns the number of milliseconds since 1970-01-01 00:00:00 UTC.
package se.wederbrand.milliseconds;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String inputString = "00:01:30.500";
Date date = sdf.parse("1970-01-01 " + inputString);
System.out.println("in milliseconds: " + date.getTime());
}
}

If you want to parse the format yourself you could do it easily with a regex such as
private static Pattern pattern = Pattern.compile("(\\d{2}):(\\d{2}):(\\d{2}).(\\d{3})");
public static long dateParseRegExp(String period) {
Matcher matcher = pattern.matcher(period);
if (matcher.matches()) {
return Long.parseLong(matcher.group(1)) * 3600000L
+ Long.parseLong(matcher.group(2)) * 60000
+ Long.parseLong(matcher.group(3)) * 1000
+ Long.parseLong(matcher.group(4));
} else {
throw new IllegalArgumentException("Invalid format " + period);
}
}
However, this parsing is quite lenient and would accept 99:99:99.999 and just let the values overflow. This could be a drawback or a feature.

Using JODA:
PeriodFormatter periodFormat = new PeriodFormatterBuilder()
.minimumParsedDigits(2)
.appendHour() // 2 digits minimum
.appendSeparator(":")
.minimumParsedDigits(2)
.appendMinute() // 2 digits minimum
.appendSeparator(":")
.minimumParsedDigits(2)
.appendSecond()
.appendSeparator(".")
.appendMillis3Digit()
.toFormatter();
Period result = Period.parse(string, periodFormat);
return result.toStandardDuration().getMillis();

If you want to use SimpleDateFormat, you could write:
private final SimpleDateFormat sdf =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
{ sdf.setTimeZone(TimeZone.getTimeZone("GMT")); }
private long parseTimeToMillis(final String time) throws ParseException
{ return sdf.parse("1970-01-01 " + time).getTime(); }
But a custom method would be much more efficient. SimpleDateFormat, because of all its calendar support, time-zone support, daylight-savings-time support, and so on, is pretty slow. The slowness is worth it if you actually need some of those features, but since you don't, it might not be. (It depends how often you're calling this method, and whether efficiency is a concern for your application.)
Also, SimpleDateFormat is non-thread-safe, which is sometimes a pain. (Without knowing anything about your application, I can't guess whether that matters.)
Personally, I'd probably write a custom method.

I am presenting two options:
Time4J, an advanced external date, time and time interval library.
java.time, the built-in modern Java date and time API.
SimpleDateFormat and Date are the wrong classes to use, both because a duration of 1 minute 30.5 seoncds is not a date and because those classes have long gone out of any reasonable use.
Time4J
This is the elegant solution. We first declare a formatter:
private static final Duration.Formatter<ClockUnit> DURATION_FORMAT
= Duration.formatter(ClockUnit.class, "hh:mm:ss.fff");
Then parse and convert to milliseconds like this:
String startAfter = "00:01:30.555";
Duration<ClockUnit> dur = DURATION_FORMAT.parse(startAfter);
long milliseconds = dur.with(ClockUnit.MILLIS.only())
.getPartialAmount(ClockUnit.MILLIS);
System.out.format("%d milliseconds%n", milliseconds);
Output is:
90555 milliseconds
java.time
The java.time.Duration class can only parse ISO 8601 format. So I am first converting your string to that format. It goes like PT00H01M30.555S (the leading zeroes are not required, but why should I bother removing them?)
String startAfter = "00:01:30.555";
String iso = startAfter.replaceFirst(
"^(\\d{2}):(\\d{2}):(\\d{2}\\.\\d{3})$", "PT$1H$2M$3S");
Duration dur = Duration.parse(iso);
long milliseconds = dur.toMillis();
System.out.format("%d milliseconds%n", milliseconds);
Output is the same as before:
90555 milliseconds
Another difference from Time4J is that the Java Duration can be directly converted to milliseconds without being converted to a Duration of only milliseconds first.
Links
Time4J - Advanced Date, Time, Zone and Interval Library for Java
Oracle tutorial: Date Time explaining how to use java.time.

Related

Calculate Number of Days between Given time in ISO format and Current time

I have to find out number of days between a given Time and current time. Given time is in ISO format and one example is "2021-01-14 16:23:46.217-06:00".
I have tried it using "java.text.SimpleDateFormat" but it's not giving me accurate results.
In Below Given date, for today's time I am getting output as "633" Days which isn't correct. somehow after parsing it is taking date as "21 december 2020" which isn't correct
String TIMESTAMP_FORMAT = "YYYY-MM-DD hh:mm:ss.s-hh:mm" ;
int noOfDays = Utility.getTimeDifferenceInDays("2021-01-14 16:23:46.217-06:00", TIMESTAMP_FORMAT);
public static int getTimeDifferenceInDays(String timestamp, String TIMESTAMP_FORMAT) {
DateFormat df = new SimpleDateFormat(TIMESTAMP_FORMAT);
try {
Date date = df.parse(timestamp);
long timeDifference = (System.currentTimeMillis() - date.getTime());
return (int) (timeDifference / (1000*60*60*24));
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
Looking for a better solution which gives me correct number of days. Thanks
Use java.time API
Classes Date and SimpleDateFormat are legacy.
Since Java 8 (which was released 10 years ago) we have a new Time API, represented by classes from the java.time package.
To parse and format the data, you can use DateTimeFormatter. An instance of DateTimeFormatter can be obtained via static method ofPattern(), or using DateTimeFormatterBuilder.
ofPattern():
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSXXX");
DateTimeFormatterBuilder:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd HH:mm:ss.") // main date-time part
.appendValue(ChronoField.MILLI_OF_SECOND, 3) // fraction part of second
.appendOffset("+HH:MM", "+00:00") // can be substituted with appendPattern("zzz") or appendPattern("XXX")
.toFormatter();
The string "2021-01-14 16:23:46.217-06:00", which you've provided as an example, contains date-time information and UTC offset. Such data can be represented by OffsetDateTime.
To get the number of days between two temporal objects, you can use ChronoUnit.between() as #MC Emperor has mentioned in the comments.
That's how the whole code might look like:
String toParse = "2021-01-14 16:23:46.217-06:00";
OffsetDateTime dateTime = OffsetDateTime.parse(toParse, formatter);
System.out.println("parsed date-time: " + dateTime);
Instant now = Instant.now();
long days = ChronoUnit.DAYS.between(dateTime.toInstant(), now);
System.out.println("days: " + days);
Output:
parsed date-time: 2021-01-14T16:23:46.217-06:00
days: 615
Note that since in this case you need only difference in days between the current date instead of OffsetDateTime you can use LocalDateTime, UTC offset would be ignored while parsing a string. If you decide to do so, then the second argument passed to ChronoUnit.between() should be also of type LocalDateTime.

how to convert string date in UTC format using joda date time

public static String convertInDateTimeSecondTOJodaTime(String dateTime) {
try {
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateTime date = formatter.parseDateTime(dateTime).withZoneRetainFields(DateTimeZone.UTC);
return date.toString("h:mm aa");
} catch (Exception e) {
return null;
}
}
main(){
print(convertInDateTimeSecondTOJodaTime("2020-04-09T07:31:16Z"))
}
I am trying to convert given date-time in UTC format using joda date time it's giving wrong time it's given one hour before please help me what I am doing wrong.
The desired result is in London time, so 8:31 AM in this case.
import java.util.Date;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
public class CurrentUtcDate {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println("UTC Time is: " + dateFormat.format(date));
}
}
Output
UTC Time is: 22-01-2018 13:14:35
You can check here https://www.quora.com/How-do-I-get-the-current-UTC-date-using-Java
As you need to you use Joda DateTime, you need to use formatter of Joda.
You are returning date with pattern "h:mm aa" so I assume you need to extract time from the date.
Below code should work:
import java.util.Locale;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class MyDateCoonverter {
public static void main(String a[]) {
System.out.println(convertInDateTimeSecondTOJodaTime("2020-04-09T07:31:16Z"));
}
public static String convertInDateTimeSecondTOJodaTime(String dateTime) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateTime dt = formatter.parseDateTime(dateTime);
return dt.toString("h:mm aa", Locale.ENGLISH);
}
}
It gives output as:
7:31 AM
If you don't want to use any third party library & still want to extract only time from date, you can use Java's LocalTime.
If you are using Java 8 or newer, you should not use java.util.Date (deprecated) or Joda Time (replaced by the new DATE API of Java 8 with java.time package) :
public static void main(String[] args) {
String date = "2020-04-09T07:31:16Z";
String formatedDate = ZonedDateTime.parse(date).format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT));
System.out.println(formatedDate); //print "7:31 AM"
}
}
First, don’t handle date and time as strings in your program. Handle them as proper date-time objects. So but for all but the simplest throw-away programs you should not want a method that converts from a string in UTC to a string in London time in a different format.
So when you accept string input, parse into a DateTime object:
String stringInput = "2020-04-09T07:31:16Z";
DateTime dt = DateTime.parse(stringInput);
System.out.println("Date-time is: " + dt);
Output so far is:
Date-time is: 2020-04-09T07:31:16.000Z
I am exploiting the fact that your string is in ISO 8601 format, the default for Joda-Time, so we need no explicit formatter for parsing it.
Not until you need to give string output, convert your date and time to the desired zone and format into the desired string:
DateTimeZone zone = DateTimeZone.forID("Europe/London");
DateTime outputDateTime = dt.withZone(zone);
String output = outputDateTime.toString("h:mm aa");
System.out.println("Output is: " + output);
Output is: 8:31 AM
What went wrong in your code
Z in single quotes in your format pattern string is wrong. Z in your input string is an offset of 0 from UTC and needs to be parsed as an offset, or you are getting an incorrect result. Never put those quotes around Z.
withZoneRetainFields() is the wrong method to use for converting between time zones. The method name means that the date and hour of day are kept the same and only the time zone changed, which typically leads to a different point in time.
What happened was that your string was parsed into 2020-04-09T07:31:16.000+01:00, which is the same point in time as 06:31:16 UTC, so wrong. You next substituted the time zone to UTC keeping the time of day of 07:31:16. This time was then formatted and printed.
Do consider java.time
As Fabien said, Joda-Time has later been replaced with java.time, the modern Java date and time API. The Joda-Time home page says:
Note that Joda-Time is considered to be a largely “finished” project.
No major enhancements are planned. If using Java SE 8, please migrate
to java.time (JSR-310).
Links
Wikipedia article: ISO 8601
Joda-Time home

How to add minutes to a time stamp that I already have?

I searched a lot, but I found a way to add or subtract time from the calendar instance which gives the current time.. how do I subtract time from the last modified time of a given file?
UPDATE :
I've been using Java1.4. That is the reason I'm unable to find any methods to do this.
I extracted the modified date as a string. I wanted to convert this string obey I a calendar object so that it's easier for me to apply add () of the calendar object to the time. I've been facing issues with the same. is this approach correct? Could you please assist
NIO and java.time
Path filePath = Paths.get("myFile.txt");
Duration timeToSubtract = Duration.ofMinutes(7);
FileTime lastModified = Files.getLastModifiedTime(filePath);
Instant lastModifiedInstant = lastModified.toInstant();
Instant timeBeforeLastModified = lastModifiedInstant.minus(timeToSubtract);
System.out.println("Time after subtraction is " + timeBeforeLastModified);
Running just now on my computer I got this output:
Time after subtraction is 2017-02-18T03:06:04Z
The Z at the end indicates UTC. Instant::toString (implicitly called when appending the Instant to a string) always generates a string in UTC.
I am using the modern Java NIO API and java.time, the modern Java date and time API. NIO gives us a FileTime in this case denoting the time the file was last modified. In order to do our time math I first convert it to an Instant, which is a central class of java.time. The minus method of an Instant subtracts a Duration, an amount of time, and returns a new Instant object.
Don’t use Calendar. That class was poorly designed and is long outdated.
Link: Oracle tutorial: Date Time explaining how to use java.time.
If you dont have to use calendar, than just use the new Java DateTime API available since Java8 https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html
There you have very nice convenience methods like plus/minus etc.
For example you can simply write
LocalTime now = LocalTime.now();
now.minusHours(2);
Timestamp (as long) you can set in Calendar instance. And after that you can add() time.
public void time() {
long timeStamp = 31415926535L;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeStamp);
// Substract 1 hour
calendar.add(Calendar.HOUR, -1);
// Add 20 minutes
calendar.add(Calendar.MINUTE, 20);
}
You can use setTime() like this:
yourTimeStamp.setTime(yourTimeStamp.getTime() + TimeUnit.MINUTES.toMillis(minutesToAdd));
Since Java 8, java.util.Date, java.util.Calendar, and java.text.SimpleDateFormat are now legacy. So I edit my codes, remove the legacy classes.
Thanks #Ole V.V. and #Basil Bourque for pointing my problems.
I'm confused about your issue. I guess you want to modify a file's last modified time.
So I write down the codes.
import java.io.File;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class Test {
private static final long ONE_SECOND = 1000L;
private static final long ONE_MINUTE = 60L * ONE_SECOND;
public static void main(String[] args) {
File file = new File("test.txt");
if (!file.exists()) {
System.err.println("File doesn't exist.");
return;
}
//get file's last modified, in millisecond
long timestamp = file.lastModified();
System.out.println("File's last modified in millisecond: " + timestamp);
//print time for human, convert to LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
System.out.println("File's last modified in LocalDateTime: " + localDateTime);
//add a minute
timestamp = timestamp + ONE_MINUTE;
//modify file's last modified
boolean isModified = file.setLastModified(timestamp);
if (isModified) {
System.out.println("Update file's last modified successfully.");
System.out.println("File's last modified in millisecond: " + file.lastModified());
//print time for human, convert to LocalDateTime
System.out.println("File's last modified in LocalDateTime: " +
LocalDateTime.ofInstant(Instant.ofEpochMilli(file.lastModified()), ZoneId.systemDefault()));
} else {
System.err.println("Update file's last modified failed.");
}
}
}
Besides, If you want modify a timestamp, just use +/- operations.
And you can convert timestamp to LocalDateTime, and use LocalDateTime's api to modify time easily.
public void modifyTime() {
//modify timestamp: add one second
long timestamp = System.currentTimeMillis();
timestamp = timestamp + 1000L;
//convert timestamp to LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
//modify LocalDateTime
//add a minute
localDateTime = localDateTime.plusMinutes(1);
//subtract a second
localDateTime = localDateTime.minusSeconds(1);
}
If I misunderstood your idea, please let me know.

String-Date conversion with nanoseconds

I've been struggling for a while with this piece of code for an Android app and I can't get the hang of it. I've read and tried every solution I found on stackoverflow and other places, but still no luck.
What I want to do is have a function to convert a string like "17.08.2012 05:35:19:7600000" to a UTC date and a function that takes an UTC date and converts it to a string like that.
String value = "17.08.2012 05:35:19:7600000";
DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss:SSSSSSS");
try
{
Date today = df.parse(value);
System.out.println("Today = " + df.format(today) + " " + today.toGMTString());
}
catch (ParseException e)
{
e.printStackTrace();
}
This results in : Today = 17.08.2012 07:41:59:0000000 17 Aug 2012 04:41:59 GMT which are both wrong.
I tried setting SDF's timezone to UTC, no luck.
Another thing that I noticed: if I do df.setLenient(false);
It gives me : java.text.ParseException: Unparseable date: "17.08.2012 05:35:19:7600000" .
If anyone can provide me with some explanations / sample code, I would be very grateful. Thanks in advance
The result you are getting is absolutely right.
Let's analyze this:
17.08.2012 05:35:19:7600000
17: Day of month (17th)
08: Month of year (August)
2012: Year (2012)
05: Hour of day (5am)
35: Minute of hour (:35)
19: Second of minute (:19)
7600000: Milliseconds of second (7,600,000)
Now, the way the VM sees this is that you are declaring the time of day as 5:35:19am, then adding 7,600,000 milliseconds to it. 7,600,000 milliseconds = 7,600 seconds = 2 hours, 6 minutes, 40 seconds. 5:35:19am + 02:06:40 = 7:41:59am (and 0 milliseconds). This is the result you are getting. (It also appears that you are not setting the timezone properly, so the GMT string is 3 hours behind your result.)
If you want to retain the :7600000, to my knowledge this is not possible. As this can be simplified into seconds, the VM will automatically reduce it into the other time increments. The milliseconds (the SSSS) should be for storing values <1000.
I'd suggest you create a new SimpleDateFormat for your output; but remember that the milliseconds will be absorbed into the other times (since they are all stored as a single long in the Date object).
private String convertDate(String cdate)
{
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss:SSSSSSS");
SimpleDateFormat postFormater = new SimpleDateFormat("yyyy-MM-dd");
Date convertedDate;
try
{
convertedDate = dateFormat.parse(cdate);
cdate = postFormater.format(convertedDate);
}
catch (ParseException e)
{
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_SHORT).show();
}
return cdate;
}
Try this.
This is what you need (but it will loose millisecond information):
"dd.MM.yyyy HH:mm:ss.'000000'"
If you used "dd.MM.yyyy HH:mm:ss.SSSSSS", then would get three leading zeros for your milliseconds.
If you used "dd.MM.yyyy HH:mm:ss.SSS'000'", then you could format a date, but not parse any date.
Try it out:
public static void main(String[] args) throws ParseException {
printDate("dd.MM.yyyy HH:mm:ss.SSS");//02.05.2010 21:45:58.073
printDate("dd.MM.yyyy HH:mm:ss.SSSSSS");//02.05.2010 21:45:58.000073
printDate("dd.MM.yyyy HH:mm:ss.SSS'000'");//02.05.2010 21:45:58.073000
printDate("dd.MM.yyyy HH:mm:ss.'000000'");//02.05.2010 21:45:58.000000
tryToParseDate("dd.MM.yyyy HH:mm:ss.SSS");//good
tryToParseDate("dd.MM.yyyy HH:mm:ss.SSSSSS");//good
tryToParseDate("dd.MM.yyyy HH:mm:ss.SSS'000'");//bad
tryToParseDate("dd.MM.yyyy HH:mm:ss.'000000'");//good
}
private static void printDate(String formatString) {
Date now = new Date();
SimpleDateFormat format = new SimpleDateFormat(formatString);
String formattedDate = format.format(now);
// print that date
System.out.println(formattedDate);
}
private static void tryToParseDate(String formatString) {
Date now = new Date();
SimpleDateFormat format = new SimpleDateFormat(formatString);
String formattedDate = format.format(now);
// try to parse it again
try {
format.parse(formattedDate);
System.out.println("good");
} catch (ParseException e) {
System.out.println("bad");
}
}
To drop the nanoseconds, use:
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.")
Update: java.time
The Question and other Answers use terrible date-time classes that are now legacy. These flawed classes were years ago supplanted by the modern java.time classes defined in JSR 310. Avoid Calendar, DateFormat, Date, etc.
Define a formatting pattern with DateTimeFormatter.
String input = "17.08.2012 05:35:19:7600000";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd.MM.uuuu HH:mm:ss:SSSSSSS" );
Parse.
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
A LocalDateTime object represents a date with a time of day, but lacks the context of a time zone or offset from UTC.
If you are certain the input text is intended to represent a moment as seen in UTC, having an offset of zero hours-minutes-seconds, then assign a ZoneOffset to produce an OffsetDateTime object.
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;
See this code run live at Ideone.com.
ISO 8601
I suggest you educate the publisher of your data about the use of ISO 8601 standard formats when serializing date-time values to text.
The java.time classes use ISO 8601 formats by default when parsing/generating text.

How can I get Date in MM/DD/YY format from Timestamp

I want to get the Date in MM/DD/YY format from a timestamp.
I have used the below method but it does not gives proper output
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(Long.parseLong(1306249409));
Log.d("Date--",""+cal.DAY_OF_MONTH);
Log.d("Month--",""+cal.MONTH);
Log.d("Year--",""+cal.YEAR);
But its gives the output like below
Date--5
Month--2
Year--1
The correct date is 24 May 2010 for Timestamp - 1306249409
Note - Timestamp is received by a webservice which is used in my application.
Better Approach
Simply Use SimpleDateFormat
new SimpleDateFormat("MM/dd/yyyy").format(new Date(timeStampMillisInLong));
Mistake in your Approach
DAY_OF_MONTH ,MONTH, .. etc are just constant int value used by Calendar class
internally
You can get the date represented by cal by cal.get(Calendar.DATE)
Use the SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String time = sdf.format(date);
What's wrong:
Calendar.DAY_OF_MONTH, Calendar.MONTH etc are static constants used to access those particular fields. (They will remain constant, no matter what setTimeInMillis you provide.)
How to solve it:
To get those particular fields you can use the .get(int field)-method, like this:
Log.d("Month--",""+cal.get(Calendar.MONTH));
As others have pointed out there are more convenient methods for formatting a date for logging. You could use for instance the SimpleDateFormat, or, as I usually do when logging, a format-string and String.format(formatStr, Calendar.getInstance()).
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy");
String s = formatter.format(date);
System.out.println(s);
TimeZone utc = TimeZone.getTimeZone("UTC"); // avoiding local time zone overhead
final Calendar cal = new GregorianCalendar(utc);
// always use GregorianCalendar explicitly if you don't want be suprised with
// Japanese Imperial Calendar or something
cal.setTimeInMillis(1306249409L*1000); // input need to be in miliseconds
Log.d("Date--",""+cal.get(Calendar.DAY_OF_MONTH));
Log.d("Month--",""+cal.get(Calendar.MONTH) + 1); // it starts from zero, add 1
Log.d("Year--",""+cal.get(Calendar.YEAR));
Java uses the number of milliseconds since 1st January 1970 to represent times. If you compute the time represented by 1306249409 milliseconds, you'll discover that it's only 362 days, so your assumptions are wrong.
Moreover, cal.DAY_OF_MONTH holds a constant. Use cal.get(Calendar.DAY_OF_MONTH) to get the day of month (same for other parts of the date).
use String.format which is able to convert long (milliseconds) to date/time string in different formats:
String str;
long time = 1306249409 * 1000L; // milliseconds
str = String.format("%1$tm/%1$td/%1$ty", time); // 05/24/11
str = String.format("%tF", time); // 2011-05-24 (ISO 8601)
str = String.format("Date--%td", time); // Date--24
str = String.format("Month--%tm", time); // Month--05
str = String.format("Year--%ty", time); // Year--11
documentation: format string.

Categories