Results 1 to 5 of 5
Thread: getting todays date on java
- 03-25-2012, 06:09 AM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
getting todays date on java
I need the month and day in Integer form, this is what I got: sorry for the bad variable names, this was just a test
import java.util.Date;
import java.util.StringTokenizer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class GetCurrentDateTime {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
//get current date time with Date()
Date date = new Date();
StringTokenizer st = new StringTokenizer(dateFormat.format(date),"/");
while(st.hasMoreTokens()){
String day = st.nextToken().toString();
String month = st.nextToken().toString();
int dayy = Integer.parseInt(day);
int monthh = Integer.parseInt(month);
Integer dayyy = new Integer(dayy);
Integer monthhh = new Integer(monthh);
when I print for example dayyy i get an error, could someone explain how I over come this
- 03-25-2012, 07:52 AM #2
Re: getting todays date on java
- 03-25-2012, 07:59 AM #3
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: getting todays date on java
It give you an error because it failed to get the next token on the second loop. The token will only have three values, date, month and year. You take two tokens on the first loop, this left one token. On the next loop you tried to read another two token but only one is available.
Btw, using the Calender object can make your code simpler to get the individual value of a date. One note that the Calender object month started with month = 0 for January.
Java Code:Calendar calendar = Calendar.getInstance(); int date = Calendar.get(Calendar.DATE); ... ...
Website: Learn Java by Examples
- 03-25-2012, 01:25 PM #4
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
Re: getting todays date on java
Hi thanks for this, sorted all my problems out but one. I am now using the calendar object. Basically I have three combo boxes, one is integer from 1-31, the other is integer form 1970-2012 and the other is string from jan to dec. I have set the year and date to the current date however the month is stuck on"Jan" could you help me change the default month to to the current month?
Thanks
No worries, all Sorted, i was using setSelectedItem instead of setSelectedIndex - my bad. ThanksLast edited by stuckonjava; 03-25-2012 at 01:30 PM.
- 03-26-2012, 05:59 AM #5
Re: getting todays date on java
try to set the selected index of the combobox as the current month you are getting from the Calendar class.
If you are not a die hard fan of combo box for picking dates, you can use JXDatePicker class available in SwingX library and can download from here
SwingLabs :: Swing Component Extensions — Java.net
Similar Threads
-
Java 7 java.util.Date vs java.sql.Date problem
By pavel_skala in forum Advanced JavaReplies: 0Last Post: 11-08-2011, 11:26 AM -
How can I know last access date & creation date for a file in java?
By Rami in forum Advanced JavaReplies: 4Last Post: 07-27-2011, 11:57 AM -
Todays Postings
By camickr in forum Suggestions & FeedbackReplies: 9Last Post: 05-23-2011, 05:55 AM -
Is there a Java function which retrieves todays name (e.g. Friday) from the OS?
By eLancaster in forum New To JavaReplies: 6Last Post: 04-02-2011, 01:49 PM -
java.util.Date vs java.sql.Date
By Jack in forum New To JavaReplies: 5Last Post: 10-28-2010, 02:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks