Adding 30 days on the value from JDateChooser using Java netbeans - java

hi all I am working on a form using JDateChooser I want to get the value of date inputted by the user. Is there any ways that after storing the value of date in a variable can I add 30 days from the inputted date? This is my code in passing the date to a string variable:
String dates =((JTextField)date.getDateEditor().getUiComponent()).getText();
my problem is how can I pass the date into a variable where i can be able to add an additional 30 days on it?
please help me really need this for my project .

To manipulate a String with a date value, you first need to convert to a Date using SimpleDateFormat, then you can perform manipulation using a Calendar.
SimpleDateFormat datefmt = new SimpleDateFormat("MM/dd/yyyy"); // Or format you're using
Date date = datefmt.parse(dates);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, 30); // Add 30 days
Date futureDate = cal.getTime();
If you need value for insertion into a database, you will of course use PreparedStatement, so you'll need a Timestamp instead:
// From Date
Timestamp futureTimestamp = new Timestamp(futureDate.getTime());
// Directly from Calendar
Timestamp futureTimestamp = new Timestamp(cal.getTimeInMillis());

Related

how to get yesterday instance from jdatechooser

The user needs to select any date (Userdate). I need to set another jdate one day behind this date (needdate). Can you help with this?
Date date1 = (Userdate.getDate(),-1);
needdate.setDate(date1)
The reason why everyone is telling you to use the new java.time package classes like LocalDate and LocalDateTime etc is because Date is quite old and known to be error prone. But hey...if you're bent on using Date then I suppose you can do it this way:
// Get the date from the JDateChooser
Date date = jDateChooser1.getDateEditor().getDate();
// Subtract one day from the selected JDateChooser date.
Date oneDayFromSelectedDate = new Date(date.getTime() - Duration.ofDays(1).toMillis());
// Define the format you want the Date String;
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
// Dump it into a String variable and...
String myDateString = sf.format(oneDayFromSelectedDate);
// Display it into the Console Window
System.out.println(myDateString);
The oneDayFromSelectedDate is the Date object containing the JDateChooser selected date less one day. Do consider changing to the java.time package.

Calculating date in mysql with java

I'm having trouble, finding out how to calculate or get the date from mysql (using java program) after 1 month of the initial saved date given below:
Date rDate = new Date();
java.sql.Date regDate = new java.sql.Date(rDate.getTime());
I am saving the date into a date column in mysql and I want to have another column which contains the date but one month ahead. In other words I have a registration date and I want to have an expiration date calculated automatically which allows only 1 month. Is it possible?
Grabbing the current date and set it into the Calendar format and add 1 to the month.
You can give this a try.
Date rDate = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(rDate);
cal.add(Calendar.MONTH, 1);
You can use Calendar class to manipulate date fields:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 1);
Date futureDate = cal.getTime();

JSON date to formatted JAVA date [duplicate]

This question already has answers here:
Java: Date from unix timestamp
(11 answers)
Closed 6 years ago.
I am trying to get dd.MM.yyyy and hh:mm from 1436536800 but only the time is correct, the date is completely wrong. I don't really understand how this is supposed to work
int dt = time.getInt("start")*1000;
Date date = new Date(dt);
startDate = dateFormat.format(date);
If time.getInt("start") is a valid unix timestamp, you must add "000" to the number. Example: 1436536800 * 1000 = 1436536800000. Then you can use the timestamp to get a Date:
final Date date = new Date(Long.parseLong("1436536800000"));
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy hh:mm");
System.out.println(sdf.format(date));
Console exit: 10.07.2015 09:00
Assuming the time is correct, it's likely the fact that you're multiplying by 1,000. When creating the date the way you are, it takes in milliseconds. Is it possible that your input is already in milliseconds? (Your current method will be ~2 minutes off if so)
Date date=new Date(1436536800);
SimpleDateFormat df2 = new SimpleDateFormat("dd.MM.yyyy");
String dateText = df2.format(date);
Date you are getting is a JSON string value. follow steps below to format it correctly.
First download Moment.js file and add it in your project.
var date1 = "1436536800"; // your long value contain in this variable.
var date2 = moment(date1).format(MMMM Do YYYY);//It will give you formatted date value.
see more formats below

JAVA modify date of String format

//Hello all,
So, I have method
public void init() {
list = taskManager.getList();
list.sort((object1, object2) -> ((String) object2.getProperties().get("bpm_startDate"))
.compareTo((String) object1.getProperties().get("bpm_startDate")));
}
This method sorts my list by the date, list gets filled from REST service, so date comes in a String format.
<p:column headerText="#{msg.date}" >
<h:outputText value="#{task.properties.bpm_startDate.substring(0,16).replace('T',' ')}">
</h:outputText>
</p:column>
this is how I "cut-off" all the redundant part of date, before displaying it to the user.
Question is, how to add 3 hours to date ?
you could use java.util.Calendar and DateFormat
//Parse String for Date
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
Date myDate = format.parse(stringDate);
//Use Calendar to add 3 hours
Calendar c = Calendar.getInstance();
c.setTime(myDate);
c.add(Calendar.HOUR_OF_DAY, 3);
//Retransform date to String
String newDate= format.format(c.getTime());
Remember to replace "yyyy/MM/dd" with your actual format following the official documentation
You can convert String date to Date using SimpleDateFormat like this
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(dateInString);
For adding Hours(time)
Check Calendar class. It has add method (and some others) to allow time manipulation. Something like this should work.
Calendar cal = Calendar.getInstance(); // creates calendar
cal.setTime(date); // sets calendar time/date
cal.add(Calendar.HOUR_OF_DAY, 1); // adds one hour
cal.getTime(); // returns new date object, one hour in the future
I would strongly suggest you check out the Joda-Time library. http://www.joda.org/joda-time/
Since I don't know the format your original date is stored in I can't provide the exact code you want, but you'll need to look at:
DateTimeFormatter - to create a formatter for your strings to a date format. Something like ISODateTimeFormat.dateTimeNoMillis().withZoneUTC() should do the trick. See http://www.joda.org/joda-time/apidocs/index.html
DateTimeFormatter.parseDateTime() - to convert the string to a DateTime
DateTime.plusHours() - see http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html#plusHours%28int%29

Java jdbc how to insert current date but formatted into database

I am using postgres 9.4(Date Column) as my database and would like to know if it is possible to insert a formatted date into the database using JDBC preparedstatement in the following format (I been searching around and couldn't find anything): Aug-21-2015. This is my code
Calendar calendar = Calendar.getInstance();
java.util.Date today = calendar.getTime();
SimpleDateFormat formatter= new SimpleDateFormat("MM-dd-yyyy");
java.util.Date mydate= formatter.parse(today.toString());
preparedStatement.setDate(2,new java.sql.Date(mydate.getTime()));
needless to say that does not work and I get this error
Unparseable date: "Fri Aug 21 01:17:59 EDT 2015"
again I am trying to get this into Aug-21-2015 , I can successfully execute the code if I only insert like this
Calendar calendar = Calendar.getInstance();
java.util.Date mydate = calendar.getTime();
preparedStatement.setDate(2,new java.sql.Date(mydate.getTime()));
but it is inserted as 8-21-2015, I was thinking of maybe just saving it as 8-21-2015 format and then just parsing all of them but would prefer the 1st option.
Your date pattern is wrong: it must be:
SimpleDateFormat formatter= new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
For mor information see the javadoc of SimpleDateFormat
A value in a date column itself is not formatted, it is just a date, if you want to format the date when you're reading it back out of the database, just use one of the date formatting functions.
E.g.
select to_char(your_date_column, 'MON-DD-YYYY') from yourtable
Don't know why you're converting to string and back, but today.toString() is NOT generating a text string that matches MM-dd-yyyy, so formatter.parse(today.toString()) will fail.
If the intent was to remove the time portion, in order to get "today", you can:
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
cal.clear();
cal.set(year, month, day);
java.sql.Date today = new java.sql.Date(cal.getTimeInMillis());
Or, with Java 8:
java.sql.Date today = java.sql.Date.valueOf(LocalDate.now());
Date Format
And for your desire the insert a "formatted date", then you are confused. A date column in the database doesn't have a format, it simply stores the date value.
When you query the date column, you get the date value, which can be retrieved in Java using the ResultSet.getDate() method.
When you query the date in a tool, the tool will convert the date to a text value for display. Some tools will allow you to control the date format.
If you otherwise want to control the date format, you can convert the date to a text column in the query, using DBMS specific formatting functions.

Categories

Resources