How to convert Large Integer 130552992000000000 into date format?
This is today’s date and timestamp.
I tried with
Date d = new Date(130552992000000000L * 1000);
System.out.println("Date : " +d);
But its showing Date : Wed Dec 29 00:28:58 IST 45183249 date which is incorrect and not showing year too.
Thanks in advance.
You can use the Calendar#setTimeInMillis(long timemillis) method and the retrieve the Date by invoking the Calendar#getTime() method.
Long l = //some value
Calendar c = Calendar.getInstance();
c.setTimeInMillis(l);
Date date = c.getTime();
Related
I've got Timestamp format in DB and my setEventDate method needs Date format.
So in my DAO class there is something like this :
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Timestamp dbDate = rows.getTimestamp("event_date");
String dbDateToString = new SimpleDateFormat("HH:mm").format(dbDate);
Date dbDateToDate = sdf.parse(dbDateToString);
e.setEventDate(dbDateToDate);
System.out.println(dbDateToString);
System.out.println(dbDateToDate);
I'm getting Timestamp from DB, format it to String and in next step I'm parsing it to Date. I know that it sounds weird. The result is:
String - 17:08
Date - Thu Jan 01 17:08:00 CET 1970
I don't get it :/ I need that "HH:mm" format.
You are taking a Timestamp from your DB, you need to change it a Date first
Timestamp dbDate = rows.getTimestamp("event_date");
Date d = new Date(dbDate.getTime();
now you can do your SimpleDateFormat formatting stuff
Timestamp dbDate = rows.getTimestamp("event_date");
Date d = new Date(dbDate.getTime());
String dbDateOnlyHourAndMinutes = new SimpleDateFormat("HH:mm").format(dbDate);
there you have your Hour and minutes only "Date".
You say you want to parse date but in your date format you are passing only hours (HH) and minutes (mm) format.
Check how to create your desired date format from :
What are the date formats available in SimpleDateFormat class?
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
example of a date format is: dd/MM/yyyy.
In case i did not understood correctly please explain more.
Here is an example code to convert the timestamp to your format.
ASSUMPTION: desired date format is MM:yyyy:
Timestamp stamp = new Timestamp(System.currentTimeMillis());//your timestamp goes here
Date date = new Date(stamp.getTime());
System.out.println(date);//result = Tue Mar 27 18:50:09 EEST 2018
SimpleDateFormat sdf = new SimpleDateFormat("MM:yyyy");
System.out.println(sdf.format(date)); //result = 03:2018
This question already has answers here:
Calendar.getInstance(TimeZone.getTimeZone("UTC")) is not returning UTC time
(7 answers)
Closed 8 years ago.
I'm trying to get an instance of Date with UTC time using the following code:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date now = cal.getTime();
that looks so simple, but if I check the values at IntelliJ's debugger, I get different dates for cal and now:
cal:java.util.GregorianCalendar[time=1405690214219,areFieldsSet=true,lenient=true,zone=GMT,firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2014,MONTH=6,WEEK_OF_YEAR=29,WEEK_OF_MONTH=3,DAY_OF_MONTH=18,DAY_OF_YEAR=199,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=30,SECOND=14,MILLISECOND=219,ZONE_OFFSET=0,DST_OFFSET=0]
now:Fri Jul 18 10:30:14 BRT 2014
as you can see, cal is 3 hours ahead of now... what am I doing wrong?
Thanks in advance.
[EDIT] Looks like TimeZone.setDefault(TimeZone.getTimeZone("UTC")); before the code above does the job...
This question has already been answered here
The System.out.println(cal_Two.getTime()) invocation returns a Date from getTime(). It is the Date which is getting converted to a string for println, and that conversion will use the default IST timezone in your case.
You'll need to explicitly use DateFormat.setTimeZone() to print the Date in the desired timezone.
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy", Locale.US);
simpleDateFormat.setTimeZone(timeZone);
System.out.println("Time zone: " + timeZone.getID());
System.out.println("default time zone: " + TimeZone.getDefault().getID());
System.out.println();
System.out.println("UTC: " + simpleDateFormat.format(calendar.getTime()));
System.out.println("Default: " + calendar.getTime());
Edit To convert cal to date
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DATE);
System.out.println(year);
Date date = new Date(year - 1900, month, day); // is same as date = new Date();
Just build the Date object using the Cal values. Please let me know if that helps.
Try using a date formatter and set the time zone to UTC.
dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
I have Date today=new Date(); which returns the current date.. but when i try to display date,month,year separately with the help of
DateFormat mmFormat=new SimpleDateFormat("MM");
System.out.println(mmFormat.format(today.getMonth()));
DateFormat yyFormat=new SimpleDateFormat("yyyy");
System.out.println(yyFormat.format(today.getYear()));
it prints month as 01 and year as 1970
how to resolve this.?
mmFormat.format(today.getMonth())
You're passing an integer – the month of the date – to a date format method.
The format method interprets that integer as a UNIX timestamp – a number of seconds since 1970.
You need to pass the date itself to the formatter.
Pass the entire date to SimpleDateFormat. The format string "MM" or "yyyy" will cause it to just extract the part of the date you want.
Just use the Date today as the input argument
System.out.println(mmFormat.format(today));
and
System.out.println(yyFormat.format(today));
today.getMonth() and today.getYear() returns an int which is interpreted as an UNIX timestamp . The value is 1 and 113 , which corresponds to approximately January 1, 1970, 00:00:01 GMT and January 1, 1970, 00:01:53 GMT represented by this Date object. To get the desired result , you need to pass the Date object :
System.out.println(mmFormat.format(today));
You would need to use Calendar. Have a look at the java docs.
You can do it like this -
Calendar cal = Calendar.getInstance();
System.out.println(cal.get(Calendar.DAY_OF_MONTH));
System.out.println(cal.get(Calendar.MONTH)); // month in the Calendar class begins from 0
System.out.println(cal.get(Calendar.YEAR));
System.out.println(cal.get(Calendar.HOUR_OF_DAY));
System.out.println(cal.get(Calendar.MINUTE));
System.out.println(cal.get(Calendar.SECOND));
This would help you to avoid creating multiple DateFormat objects. Also in case you want to use another date instead of today's date the you can just pass the date to the cal.setTime() method.
That is because all these methods are deprecated. Use
Calendar myCalendar = GregorianCalendar.getInstance();
myCalendar.get(Calendar.MONTH);
myCalendar.get(Calendar.YEAR);
myCalendar.get(Calendar.DAY_OF_MONTH);
Better in this way
Date date=new Date(); // your date
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
System.out.println(year+"\n"+month);
I am using Selenium Webdriver for automation and need to retrieve the current age of a person to compare it with the age populated in the application.
My Code goes like :
String DOB = driver.findElement(By.id("")).getAttribute("value");
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
Date convertedDate = dateFormat.parse(DOB);
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
Date currentNow = currentDate.getTime();
System.out.println("Sys date: " + currentNow);
System.out.println("DOB Date: " + convertedDate);
Output:
Sys date: Tue Mar 05 12:25:19 IST 2013
DOB Date: Wed Mar 15 00:00:00 IST 1967
How can I retrieve the proper age so that I can compare it with application's age being auto-populated. Currently when we subtract by using .getYear() it is assuming the date of the year starting from Jan 1, hence not calculating the proper age.
Please help me on this so that I can successfully calculate the correct age.
If you're already comparing years, why not compare the Month/Day to the current one? The Calendar can do this for you with a little bit of coaxing.
//Retrieve date from application
String DOB = driver.findElement(By.id("")).getAttribute("value");
//Define the date format & create a Calendar for this date
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
Calendar birthday = Calendar.getInstance();
birthday.setTime(sdf.parse(DOB));
//Create a Calendar object with the current date
Calendar now = Calendar.getInstance();
//Subtract the years to get a general age.
int diffYears = now.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
//Set the birthday for this year & compare
birthday.set(Calendar.YEAR, now.get(Calendar.YEAR));
if (birthday.after(now)){
//If birthday hasn't passed yet this year, subtract a year
diffYears--;
}
Hope this helps.
Please check whether this helps you.
This method will give exact years in numbers.
public static int getDiffYears(Date first, Date last) {
Calendar a = getCalendar(first);
Calendar b = getCalendar(last);
int diff = b.get(YEAR) - a.get(YEAR);
if (a.get(MONTH) > b.get(MONTH) ||
(a.get(MONTH) == b.get(MONTH) && a.get(DATE) > b.get(DATE))) {
diff--;
}
return diff;
}
I have a string (Jan12) (generated by applying some operations on current date {20-jan-2012}) Now i want to convert back this string into Date format . Also the value should be same i.e the new Date object should have value jan12 and not (20-jan-2012) . Pls help . I have tried doing
java.sql.Date.valueOf("Jan12") [this throws IllegalArgumentException]
and also
new SimpleDateFormat("MMMyy").parse("Jan12") [By this Date gets converted to 20-jan-2012]
Output required : A Date Object having value Jan12 (12 is the year)
My Code : new java.text.SimpleDateFormat("MMMyy").format(new java.text.SimpleDateFormat("yyyy-MM-dd").parse(s)) // It is a string which gives Jan12
Now i really want to convert Mycode into a Date object
Date now = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String s1 = df.format(now);
System.out.println(s1); // 2012-01-20
java.sql.Date d111=java.sql.Date.valueOf(s1);
System.out.println(d111); // 2012-01-20
DateFormat df1 = new SimpleDateFormat("MMMyy");
String s2 = df1.format(d111);
System.out.println(s2); //Jan12
Now i want s2 to be converted in Date object
#Aditya,
If you use the Str2 which gives "Jan12", there is no date part in that string and therefore if you convert it to a date object, it will get "Jan" as month, 12 as year but it cant find "day" in that String.
if you use below code
try
{
Date d2 = df1.parse(s2); //here s2 is your string which gives "JAN12"
System.out.println(d2);
}
catch(ParseException pe)
{
System.out.println("parse exception..");
}
The output to the above code will be:
Sun Jan 01 00:00:00 IST 2012
notice here that day part is reset to the first day of the month
Therefore, it is not possible to get a complete date object as your original Date, the month and year are preserved, but the day part is lost.
What do you mean "gets converted"? How your Date is displayed is a separate issue. Look into formatting a Date.
So the 12 is day, not a year - you should parse it as such. Aslo, you'll need to tell it what year this is:
System.out.println(new SimpleDateFormat("yyyyMMMdd").parse("2012" + "Jan12"));
Output
Thu Jan 12 00:00:00 EST 2012
Use the SimpleDateFormat class properly, it will do exactly what you want
String str_date="12-Jan-2012";
DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date date = (Date)formatter.parse(str_date);
Note: the formatter.parse() method throws ParseException, catch it;
If 12 is a year
Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("MMMyy").parse("Jan12"));
calendar.set(Calendar.DATE, 1);
Date date = calendar.getTime(); // First Jan 2012
If 12 is a day
Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("MMMdd").parse("Jan12"));
calendar.set(Calendar.YEAR, 2012);
Date date = calendar.getTime(); // 12 Jan 2012
I understand that you want to format your Date object into a String representation.
You can use SimpleDateFormat for this, analog to your second example:
Date d = new Date(112, 0, 20); //don't construct a date like this in production code, use a Calendar instance instead
String formattedDate = new SimpleDateFormat("MMMyy").format(d); // -> "Jan12"
Note that your Date object represents a specific point in time, it will always have a day and a time associated with it.
If you want to compare Dates with the resolution of a month, you have to set day and time to neutral values:
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
d = cal.getTime();
Just extend Date and customize it to use your favourite parse & format methods.