I wonder why the setTime method behaves exactly like setDate, date without time, or instead to set the time on 2014-07-01 13:21:01 it is set on 2014-07-01 00:00:00 ?!?!
Is setTime deprecated?
Should I use setTimestamp???
Databases other than Oracle actually do distinguish between three different datatypes:
DATE only date, no time
TIME only time of the day, no date
TIMESTAMP both, date & time.
JDBC tries to abstract standard SQL concepts and the above three datatypes are defined by ANSI SQL and thus JDBC needs to support them.
As Oracle's date always includes the time, you have to use setTimestamp() otherwise the time is lost when you store it in the database.
setTime() Method :
The java.util.Calendar.setTime(Date) method sets Calendar's time with the given Date.
Following is the declaration for java.util.Calendar.setTime() method
public final void setTime(Date date)
This method does not return a value.
Example :
The following example shows the usage of java.util.calendar.setTime() method.
package com.tutorialspoint;
import java.util.*;
public class CalendarDemo {
public static void main(String[] args) {
// create a calendar
Calendar cal = Calendar.getInstance();
// get the current time
System.out.println("Current time is :" + cal.getTime());
// create new date and set it
Date date = new Date(95, 10, 10);
cal.setTime(date);
// print the new time
System.out.println("After setting Time: " + cal.getTime());
}
}
SetTimestamp Method :
Sets the designated parameter to the given timestamp and calendar values.
Syntax
public void setTimestamp(java.lang.String sCol,
java.sql.Timestamp x,
java.util.Calendar c)
Related
I'm a little confused using the DateTime related class for Java SE 7 and 8 API's, for displaying the current time, I'm reviewing the multiple ways for get the system's current datetime.
My question is: Which one is more accurate for displaying time in millis?
Next is the code snippet, I'm using Java 8 for the reviewing.
import java.time.Instant;
import java.util.Calendar;
import java.util.Date;
public class CurrentTimeValidationDemo {
public static void main(String[] args) {
Instant now = Instant.now();
Calendar calendar=Calendar.getInstance();
Date currDate = new Date();
System.out.println("new Date().getTime() = "+currDate.getTime());
System.out.println("System.currentTimeMillis() = "+System.currentTimeMillis());
System.out.println("Instant.now().toEpochMilli() = "+now.toEpochMilli());
System.out.println("Calendar.getInstance().getTimeInMillis() = "+calendar.getTimeInMillis());
System.out.println("Calendar.getInstance().getTime().getTime() = "+calendar.getTime().getTime());
}
}
All the functions that you used in your example return the same value (if launched in the same millisecond) none of them is more accurate.
Once of them is not creating any object, so if you need only to know the current milliseconds since 1/1/1970 use
System.currentTimeMillis()
Instead if you need to have also the equivalent object to store that value or to make additional operations use the object that you need.
For example if you need to pass this value to a function accepting a java.util.Date use java.util.Date (and so on).
I'm calling in JS some javaMethod(java.util.Date date).
I can't change code on java-side. How can I create java.util.Date object from JS-side (at least empty date)?
There is no way to use java date in javascript use javascript Date
var date = new Date();
Just get the long value from javascript date and pass to java.
in javascript get time in milliseconds as
var timeinmillis = new Date().getTime();
and pass this to java.
in java , look at the constructor
public Date(long date)
Solved it in a not very good way, but still..
So now I'm just added to that method with JS just one more Date arg.
Now it looks like
native void doSomething(Object object, Date date) /*-{
.....
.....::setDate(Ljava/util/Date;)(date);
.....
}-*/;
and calling it like doSomething(var1, new Date());
Why does this code return 0001-02-05?
public static String getNowDate() throws ParseException
{
return Myformat(toFormattedDateString(Calendar.getInstance()));
}
I changed the code to:
public static String getNowDate() throws ParseException
{
Calendar temp=Calendar.getInstance();
return temp.YEAR+"-"+temp.MONTH+"-"+temp.DAY_OF_MONTH;
}
And now it returns 1-2-5.
Please, help me get the actual date. all i need is the Sdk date.
Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH are int constants (just look up in the API doc)...
So, as #Alex posted, to create a formatted String out of a Calendar instance, you should use SimpleDateFormat.
If however you need the numeric representations of specific fields, use the get(int) function:
int year = temp.get(Calendar.YEAR);
int month = temp.get(Calendar.MONTH);
int dayOfMonth = temp.get(Calendar.DAY_OF_MONTH);
WARNING! Month starts from 0!!! I've made some mistakes because of this!
Use SimpleDateFormat
new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
You are using constants to be used with the Calendar.get() method.
Why not use SimpleDateFormat?
public static String getNowDate() {
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
}
You are doing it wrong. Change to:
return temp.get(Calendar.YEAR)+"-"+ (temp.get(Calendar.MONTH)+1) +"-"+temp.get(Calendar.DAY_OF_MONTH);
Also, you may want to look into Date:
Date dt = new Date();
//this will get current date and time, guaranteed to nearest millisecond
System.out.println(dt.toString());
//you can format it as follows in your required format
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(dt));
I'm working on a method that can tell me if a certain date is within a certain period of dates. My method will have an argument of two DateTime objects; a start date and end date, and will be called by a DateTime object as well.
To play around with it, I've been trying to figure out how to extract the year, month, day , time, from a DateTime object that is being compared to. However I can't figure out how to get it going. I checked the API for DateTime, and the method it has to perform the function I want is monthOfYear().
But when I implement it, it outputs "Property[monthOfYear]".
The API places the method under DateTime.Property but I played around with that too and I'm not getting anywhere.
import org.joda.time.DateTime;
public class Tester implements TesterInterface {
public static void main(String[] args) {
DateTime dateTime1 = new DateTime(2012, 5, 12, 13, 30);
System.out.println(dateTime1.monthOfYear());
}
}
Call getMonthOfYear().
As documented:
Each individual field can be queried in two ways:
getHourOfDay()
hourOfDay().get()
Change
dateTime1.monthOfYear()
to
dateTime1.getMonthOfYear()
I want to set the fields of a given source date to a given target date using this method.
private static void setFields(final Date source,
final Date target,
final int ... fields)
{
final Calendar sourceCalendar = Calendar.getInstance();
sourceCalendar.setTime(source);
final Calendar targetCalendar = Calendar.getInstance();
targetCalendar.setTime(target);
for(int field : fields)
targetCalendar.set(field, sourceCalendar.get(field));
}
The minute of the target Date is not set.
How am I suppose to set it if I don't want to break the reference by using.
target = targetCalendar.getTime();
To change the target date to the value in the targetCalendar, use:
target.setTime(targetCalendar.getTime().getTime());
Assuming that's what you were asking, your question was a little unclear.