I am running one query on oracle sql which returns me timestamp part of of sysdate in string
something like "16:30:0.0"
so i want to know how to convert it to milliseconds.
please help?
This is using the standard Java Date API.
DateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
df.parse("16:06:43.233").getTime();
If you're using Java 8, see the new java.time API. If not, and you're going to do a lot of date-time-related work, see JodaTime
Use ResultSet's getTime(column)-method instead of getString(column) to avoid having to do the conversion yourself: http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getTime%28int%29
Try this,
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
Date date = sdf.parse("16:30:0.0");
System.out.println(date.getTime());
But, this code will return milliseconds since 01.01.1970 16:30:00.000. If you want to get millis from the current day you can do the following.
Calendar today = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.YEAR, today.get(Calendar.YEAR));
cal.set(Calendar.MONTH, today.get(Calendar.MONTH));
cal.set(Calendar.DAY_OF_MONTH, today.get(Calendar.DAY_OF_MONTH));
System.out.println(cal.getTimeInMillis());
Related
I have hour and minute in edittext.(say for example 10:50)
how to get today's DateTime(like yyyy-MM-dd HH:mm:ss) based on above edit text value 10:50
UPDATE:
this worked for me:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, selectedHour);
calendar.set(Calendar.MINUTE, selectedMinute);
thank you all
EDIT: take a look at this other stackoverflow post about why one should prefer using the Java 8 java.time classes over Calendar or Date
In your instance, in order to parse an input in the form "HH:mm", you can create a DateTimeFormatter and use the static method LocalTime.parse(inputString, dateTimeFormatter) to get a LocalTime object
For example:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
LocalTime time = LocalTime.parse(timeInputString, formatter);
Take a look at the Java documentation for DateTimeFormatter for more information on the patterns for years, days, or whatever.
If you're wanting to get both the date and time from a string such as "10:50", you won't be able to using the LocalDateTime.parse method because it can't obtain the date from an "HH:mm" string. So the way around this is to create the time off of LocalTime.parse as shown above, and then get the current date using the static method LocalDate.now(); And then combine those two results into a LocalDateTime object using it's static factory method LocalDateTime.of(date, time)
For example:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
LocalTime time = LocalTime.parse(timeInputString, formatter);
LocalDate date = LocalDate.now();
LocalDateTime dateTime = LocalDateTime.of(date, time);
Calendar cal = new Calendar.getInstance();// today's date
cal.set(Calendar.HOUR, 10);
cal.set(Calendar.MINUTE, 50);
cal.set(Calendar.SECOND, 0);
System.out.println(cal.getTime()); //see the result
now , it's up to you to put them directly in cal.set with variables (not manually writing 10 ,50 , it's up to your creativity )
And this answer is with Calendar because you tagged Calendar in your question , you can also use something else than Calender
you just need use this function.
// hourMintText = "hh:mm"
private String getTodayAsFormat(String hourMintText){
String[] hourMinAsArray = hourMintText.split(":");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hourMinAsArray[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(hourMinAsArray[1]));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm", Locale.ENGLISH);
return simpleDateFormat.format(calendar.getTimeInMillis());
}
Try this:
String dateTime = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss aa", Locale.getDefault())
.format(new Date());
How to reduce one month from current date and want to sore in java.util.Date variable
im using this code but it's shows error in 2nd line
java.util.Date da = new Date();
da.add(Calendar.MONTH, -1); //error
How to store this date in java.util.Date variable?
Use Calendar:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
Date result = cal.getTime();
Starting from Java 8, the suggested way is to use the Date-Time API rather than Calendar.
If you want a Date object to be returned:
Date date = Date.from(ZonedDateTime.now().minusMonths(1).toInstant());
If you don't need exactly a Date object, you can use the classes directly, provided by the package, even to get dates in other time-zones:
ZonedDateTime dateInUTC = ZonedDateTime.now(ZoneId.of("Pacific/Auckland")).minusMonths(1);
Calendar calNow = Calendar.getInstance()
// adding -1 month
calNow.add(Calendar.MONTH, -1);
// fetching updated time
Date dateBeforeAMonth = calNow.getTime();
you can use Calendar
java.util.Date da = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(da);
cal.add(Calendar.MONTH, -1);
da = cal.getTime();
Using new java.time package in Java8 and Java9
import java.time.LocalDate;
LocalDate mydate = LocalDate.now(); // Or whatever you want
mydate = mydate.minusMonths(1);
The advantage to using this method is that you avoid all the issues about varying month lengths and have more flexibility in adjusting dates and ranges. The Local part also is Timezone smart so it's easy to convert between them.
As an aside, using java.time you can also get the day of the week, day of the month, all days up to the last of the month, all days up to a certain day of the week, etc.
mydate.plusMonths(1);
mydate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)).getDayOfMonth();
mydate.with(TemporalAdjusters.lastDayOfMonth());
Using JodaTime :
Date date = new DateTime().minusMonths(1).toDate();
JodaTime provides a convenient API for date manipulation.
Note that similar Date API will be introduced in JDK8 with the JSR310.
You can also use the DateUtils from apache common. The library also supports adding Hour, Minute, etc.
Date date = DateUtils.addMonths(new Date(), -1)
raduce 1 month of JDF
Date dateTo = new SimpleDateFormat("yyyy/MM/dd").parse(jdfMeTo.getJulianDate());
Calendar cal = Calendar.getInstance();
cal.setTime(dateTo);
cal.add(Calendar.MONTH, -1);
Date dateOf = cal.getTime();
Log.i("dateOf", dateOf.getTime() + "");
jdfMeOf.setJulianDate(cal.get(Calendar.DAY_OF_YEAR), cal.get(Calendar.DAY_OF_MONTH),
cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
1.I want to set the setMaxSelectableDate=18years in JDateChooser so i provided it the date by incrementing milliseconds but how should i increment it by 18years.
2.Incrementing by 18years the calculation comes out to be 365*18*24*60*60*1000=56764800000 which gives me error integer number to large.
Date max=new Date();
Date oth1=new Date(max.getTime() + (365*18*24*60*60*1000)); //days*hours*minutes*seconds*milliseconds
SimpleDateFormat maxdateFormatter1 = new SimpleDateFormat("MMM d,yyyy hh:mm:ss a");
String maxdate=maxdateFormatter1.format(oth1);
DateChooser_V1.setMaxSelectableDate(new java.util.Date(maxdate));
Let java.util.Calendar do this work for you:
Calendar c = Calendar.getInstance();
c.setTime(oldDate);
c.add(Calendar.YEAR, 18);
Date newDate = c.getTime();
Which takes care of leap years, historical GMT offset changes, historical Daylight Saving Time schedule changes etc.
You need to use a long. You can achieve this by adding an L to your number:
365L* ...
With JodaTime
DateTime in18Years = new DateTime( ).plusYears( 18 );
Here is how to convert to java.util.Date
Date in18Years = new DateTime( ).plusYears( 18 ).toDate( );
You cannot willy-nilly add seconds (or millseconds) and expect calendar calculations to come out right. Basically it takes some extra effort to account for all of those leap-years, leap seconds, and daylight savings shifts.
Until Java 1.8 comes out, use java.util.Calendar instead of java.util.Date, there are really good reasons that java.util.Date has practically everything in it deprecated. While it looks good in the beginning, with enough use you will find it often "just doesn't work (tm)".
GregorianCalendar now = new GregorianCalendar();
now.add(Calendar.YEAR, 18);
And that's assuming that you didn't overflow Integer.MAX_INT.
I would use a Calendar object to achieve this:
Calendar cal = Calendar.getInstance();
Date dt = new Date();
...
// Set the date value
...
cal.setTime(dt);
cal.add(Calendar.YEAR, +18);
dt = cal.getTime();
Hope this helps you
How to convert Formatted date (yyyy-MM-dd) to Unix time in Java?
I want to declare a date using
Date birthday = new Date(y_birthday, m_birthday, d_birthday);
but this constructor has been deprecated, so I got to use the other constructor which uses Unix timestamp
So, you have the date as a string in the format yyyy-MM-dd? Use a java.text.SimpleDateFormat to parse it into a java.util.Date object:
String text = "2011-12-12";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(text);
edit If you need a java.sql.Date, then you can easily convert your java.util.Date to a java.sql.Date:
java.sql.Date date2 = new java.sql.Date(date.getTime());
Use a calendar object if you want more control of the date object
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2011);
calendar.set(Calendar.MONTH, 11); // indexed month (December)
calendar.set(Calendar.DATE, 12);
Date date = new Date(calendar.getTime().getTime());
The hours, minutes, seconds etc of the current time will be set though so you may want to set those to 0 (manually per field)
If you're using Java 7 then I think there's some much nicer stuff you can use for handling dates
The database has data in UTC and when I try to get data
java.util.Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
java.sql.Timestamp ts = resultSet.getTimestamp(PUBLISH_TIME);
cal.setTime(ts);
Is there anything wrong with this?
java.util.Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
java.sql.Timestamp ts = resultSet.getTimestamp(PUBLISH_TIME, cal);
This should do the trick!
Your DateFormat instance is most likely displaying the value in local time. When displaying your value, give this a try:
java.util.Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
java.sql.Timestamp ts = resultSet.getTimestamp(PUBLISH_TIME);
cal.setTime(ts);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(sdf.format(cal.getTime()));
EDIT: to your comment:
What if I use GMT, would that be an issue in SimpleDateFormat
SimpleDateFormat can use general timezones (GMT +/- n), RFC822, and text ("if they have names" as the JavaDoc states - see this post for info on the names).