I recently started supporting a system/application written in java.
I need to convert the below long date to a readable date as in 21 October 2016 :
Login date : 634940995544109969
Logout date : 63494125060775764
I tried different codes,I don't seem to come right.
A solution can be in java or c#.
You must add L at the end of the input .
Try following code .
public static void main(String[] args) {
long val = 634940995544109969L;
Date date=new Date(val);
System.out.println(DateFormat.getDateInstance().format(date));
}
The output will be in readable format .
For Ex : The above code will give Mar 5, 20122449 as output .
The milli seconds are converted into date.
You can check the correctness of output in the below link
Milliseconds to Date Conversion
You can also convert using below code
public static void main(String[] args) {
long lMilliSeconds = 634940995544109969L;
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(lMilliSeconds);
System.out.println(cal.getTime());
}
Note-: I have considered that time is given in milliseconds.
Output-: Fri Mar 05 07:45:09 IST 20122449
try following in c#
long a = 634940995544109969;
DateTime dt = new DateTime(a);
Console.WriteLine(dt.ToString("dd MMM yyyy"));
Related
I'm getting inconsistent results when converting Dates to LocalDates, around the year 200. Using the following code to do the conversion:
private LocalDate toLocalDate(Date localDate)
{
return LocalDateTime.ofInstant(localDate.toInstant(), ZoneId.systemDefault()).toLocalDate();
}
My ZoneId.systemDefault() is Africa/Harare, which matches the CAT used in the test. The test case I run is
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
String dateString = "Tue Jan 01 00:00:00 CAT 200";
String dateString2 = "Tue Jan 01 00:00:00 CAT 201";
String dateString3 = "Wed Dec 31 00:00:00 CAT 200";
System.out.println(toLocalDate(simpleDateFormat.parse(dateString)));
System.out.println(toLocalDate(simpleDateFormat.parse(dateString2)));
System.out.println(toLocalDate(simpleDateFormat.parse(dateString3)));
My expected output for this would be
0200-01-01
0201-01-01
0200-12-31
Or, if not that, at least consistently incorrect values. The actual results are
0199-12-31
0201-01-01
0200-12-31
So it seems that the first one is being rolled back slightly, possibly the two hours corresponding to the CAT timezone? But why does this only happen on the one case? Doing the same experiment with the year 2000 does not produce the same error.
Stephen has provided an explanation in the comment. Basically, java.util.Date uses a calendar system which cuts over between the Julian calendar system and the Gregorian calendar system in 1582, skipping 10 days. So dates in 1582 or before will exhibit discrepancies - but the size of the discrepancy will vary over time - by 3 days every 400 years, on average. It so happens that between 200 and 400AD, you don't see this because that corresponds to when the discrepancy is 0.
Here's a short but complete program to demonstrate the problem:
import java.time.*;
import java.util.*;
public class Test {
public static void main(String[] args) throws Exception {
// Value obtained with Noda Time: should be 0199-12-31T22:00:00Z.
long millis = -55855792800000L;
Instant instant = Instant.ofEpochMilli(millis);
Date date = new Date(millis);
System.out.println(instant);
System.out.println(date);
}
}
Output on my machine:
0199-12-31T22:00:00Z
Tue Jan 01 22:00:00 GMT 200
This is all complicated by the problems in your initial code of assuming CAT and Africa/Harare are the same (at that point in time, Africa/Harare is regarded as having an offset of +02:10) and the incorrect day names in your strings - but it's the bug in Java which is causing the issue here.
I suggest you perform all your parsing using the java.time.format classes - then I'd hope you won't get this inconsistency.
I try to convert date time zone to another time zone, I tried with the following code to convert, but it doesn't work.
I'm using JDK 1.3. My goal is to have the same time "Wed May 6 10:08:54 BST 2015" but with a different timezone stamp. So my expected output is: "Wed May 6 10:08:54 IST 2015". I don't want to calculate the difference.
My code:
public static void main(String[] args) {
try{
Test obj= new Test();
String date="Wed May 6 10:08:54 BST 2015";
System.out.println("Given Date = "+date);
Date dt=obj.getServerDate(date);
System.out.println("Returned Date = "+dt);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public Date getServerDate(String str_date)
{
if (str_date == null)
return null;
Date pars_date = null;
try
{
DateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.ENGLISH);
pars_date = sdf.parse(str_date);
sdf.setTimeZone(TimeZone.getTimeZone("IST"));
}
catch(Exception e)
{
System.out.println(e);
}
return pars_date;
}
When I execute the above code I got the following output:
Given Date = Wed May 6 10:08:54 BST 2015
Returned Date = Wed May 06 14:38:54 IST 2015
The returned date is mismatched, I tried lot to solve this problem, but I couldn't. Share your ideas to solve this problem.
Note: I don't want Joda Time calendar
Thanks in advance.
Your returned time is correct BST is +1:00 and IST is +5:30 so when it is 10:08:54 in London it is 14:38:54 in New Delhi.
If you want to change the timezone without changing the time/date and don't want to use Joda you'll need to first parse the string to a date object, then rebuild a NEW date object using values from the old one but inject your own timezone. Changing the timezone on the date formatter will also change the time/date.
P.s. See Changing timezone without changing time in Java
I want to get 12 hour clock time from milliseconds.I tried as follows
public class GetTimeFormat{
static SimpleDateFormat format;
public static String convertDate(String dateformat,Long date){
format = new SimpleDateFormat(dateformat);
String formattedDate = format.format(date);
return formattedDate;
}
public static void main(String args[])
{
long cal=1386059340010l;
String dateString=convertDate("MMM dd,yyyy HH:mm:ss a", cal);
System.out.println(dateString);
}
}
The corresponding date for the above milliseconds is Tue Dec 03 13:59:00 IST 2013
So I thought I will get formatted date as Dec 03,2013 1:59:00 PM
but instead I am getting Dec 03,2013 13:59:00 PM
there is no need for am/pm in 24 hour clock and in 12 hour clock am/pm is required
But In my way I am getting time in 24 hour format + PM.
Can Any body tell me whats the mistake here?
Another question is why in ideone its showing Dec 03,2013 08:29:00 AM
Not only in ideone but I have checked many online compilers and every where its showing the same but in local machine time is different(13:59)
You need to use a lowercase h in your format pattern:
String dateString = convertDate("MMM dd,yyyy h:mm:ss a", cal);
You can see here for a full reference of format patterns, including an example that covers this specific case.
I want to convert a date to a long value (that is the milliseconds)
I have a date like
2/11/2014
I want to calculate the date in long (manual)
What I've tried
(2014 - 1970 ) * 31449600000 + 11 * 2592000000 + 2 * 604800000
This equals 1413504000000.
But http://www.fileformat.info/tip/java/date2millis.htm tells me that 1413504000000 is
Date (America/New_York) Thursday, October 16, 2014 8:00:00 PM EDT
Date (GMT) Friday, October 17, 2014 12:00:00 AM GMT
Date (short/short format) 10/16/14 8:00 PM
Where I'm wrong?
Again, I want to do this manually, not using java code.
Do not re-invent the wheel. Time/date calculations are notoriously difficult, even standard java library does not get it right. Use JodaTime:
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class JodaTimeSample {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy");
DateTime date = DateTime.parse("2/11/2014", formatter);
System.out.println("Date: " + date.toString());
System.out.println("Millis: " + date.getMillis());
}
}
If you are sure you could do it manually (huh, why? - looks like homework), open JodaTime source code and copy it. You won't invent it better. Or even better open, read and then try to write it in your editor.
why you do manually to convert date into long or long into date, write a simple java code which give you correct results after convention, I am using the simple programme and convert it as per my user
public class test {
public static void main(String[] args) {
Date dt=new Date(Long.valueOf(1390973400983L));
System.out.println(dt.toString());
Calendar cal=Calendar.getInstance();
cal.set(2014, Calendar.JANUARY, 29, 11, 00, 0);
System.out.println(cal.getTimeInMillis());
System.out.println();
}
}
for more information visit here
I have a XMLGregorianCalendar that I would like to convert to a Java Date object, but when I try to covert this:
2013-11-19T00:00:00-00:00
I always get a date with the value a day behind.
Mon Nov 18 17:00:00 MST 2013
I just want a date object containing 11/19/2013.
As commented above, the result you're getting is right - it's the same moment in time. Midnight UTC is 5PM MST the day before. Perhaps you should look into why your time is in "-00:00" instead of "-07:00" (MST)... but in the meanwhile, I suppose you could try this:
public static void main(String... args) throws DatatypeConfigurationException {
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar("2013-11-19T00:00:00-00:00");
Calendar c = xcal.toGregorianCalendar();
c.setTimeZone(TimeZone.getDefault());
Date d = c.getTime();
System.out.println(d);
}
prints out Tue Nov 19 00:00:00 EST 2013, and will work for other times of day, not just midnight.
I just did this,
Date startDate = new Date(request.getStartTime().getYear(), request.getStartTime().getMonth(), request.getStartTime().getDay(), 0, 0, 0);