Jump to content
xisto Community
Sign in to follow this  
suicide1405241470

Using system date in java... How?

Recommended Posts

How do u use system date in java?I only know how in jsp. In jsp, u need to do the code below to get the sysdate<%@ page import="java.text.*, java.util.Date" %>String date = DateFormat.getInstance().format(new Date());and you will be able to get the system date which has the format 6/6/04 7:19 AM______________________iv tried to do this in java import java.util.Date;public String getSysDate() { String date = ""; String date = DateFormat.getInstance().format(new Date()); return date; }there's an error on the variable DateFormat., it cannot resolve symbol.does anyone know how?

Share this post


Link to post
Share on other sites

How do u use system date in java?

I only know how in jsp. In jsp, u need to do the code below to get the sysdate

<%@ page import="java.text.*, java.util.Date" %>String date = DateFormat.getInstance().format(new Date());

and you will be able to get the system date which has the format 6/6/04 7:19 AM

 

______________________

 

iv tried to do this in java

import java.util.Date;public String getSysDate()    {    String date = "";                      String date = DateFormat.getInstance().format(new Date());    return date;    }

there's an error on the variable DateFormat., it cannot resolve symbol.

does anyone know how?

<{POST_SNAPBACK}>


That's because the import for the DateFormat is missing; DateFormat is not in default package (java.lang) so you have to import it as you did it in your jsp either by

import java.text.*;
or by

import java.text.DateFormat;
in the import section then it should work....

 

 

Cheers...

 

JDrive

Share this post


Link to post
Share on other sites

import java.util.Date;public String getSysDate()    {    String date = "";                      String date = DateFormat.getInstance().format(new Date());    return date;    }

--> The last post explained successfully why your code couldn't find the command as you didnot declare the exclusive namespace of the package. Another problem your could face here is casting the date from Date type to String. You have to use the toString() method which forms a part of every cannonical class in java. Try the following code instead:==========================================
import java.util.Date;

public string getSysDate {

Date sysDate = new Date ();
String sysDateStr = sysDate.toString ();
return sysDateStr;

}
==========================================

There are several other methods to perform the same. Here's the code:

import java.text.SimpleDateFormat;
import java.util.Calendar;

public string getSysDate {

Calendar sysDate;
String sysDateStr;

sysDate = Calendar.getInstance();
sysDateStr = new SimpleDateFormat("dd-mmm-yy").format(curDate).toString();

return sysDateStr;
}

==========================================

Yet, a third method:

import java.text.SimpleDateFormat;
import java.util.Date;

public string getSysDate {

// Make sure the Month ("MM") in the "MM-dd-yyyy" is in CAPITALS
SimpleDateFormat DateFormat = new SimpleDateFormat("MM-dd-yyyy");

Date today = new Date();

return DateFormat.Format (today).toString ();

}

Any of them should do your job fine....
.:: Cheers ::. :)

Share this post


Link to post
Share on other sites

Current Sysdate using Java

Using system date in java... How?

 

Import java.Util.Date;

import java.Text.DateFormat;

import java.Text.SimpleDateFormat;

 

private String getDateTime() {

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.Ms");

Date date = new Date();

System.Out.Println(dateFormat.Format(date));

}

 

You can format the date as per your requirement. Here I have tried to print the milliseconds also...Keep Rocking!!

 

-reply by niranjan

Share this post


Link to post
Share on other sites

Get the Locale & DateTime

Using system date in java... How?

 

Package org.Glen.Helloworld;

Import java.Util.Date;

Import java.Util.Locale;

Import java.Text.DateFormat;

 

Public class HelloMain {

 

public static void main(String[] args) {

System.Out.Println("Locale: " + Locale.GetDefault());

System.Out.Println("DateTime: " + DateFormat.GetInstance().Format(new Date()));

}

}

 

-reply by glen

Share this post


Link to post
Share on other sites
Date outputUsing system date in java... How?

I find this method the simplest and clear of all.

You will need:

import java.Text.DateFormat; /*--to format the date and time(formatting subclasses)*/

import java.Util.Date; /*specific instant in time*/

Creating a simple function like below would be handy;

  public String getdate(){     String mydate = DateFormat.GetInstance().Format(new Date());     return mydate;

 

}

-reply by Caleb Nahabwe

Share this post


Link to post
Share on other sites
logtrail date problemUsing system date in java... How?I have a database (access). I inserted data (date, username, login and logout). I called the system time and successfully inserted those data in my database each time a user logs in.The problem is each time I log out, the time of my log-out don't insert on the column I logged in earlier. It looks like this:Date username Login Logou09/05/09 sam 08:30PM 10:30PMWhat should I do?

Share this post


Link to post
Share on other sites
Java tymUsing system date in java... How?

 please I am a beginner but hw wuld I insert that time code in the code it seems tough and given me error please someone help

-reply by Oladeji

 

Share this post


Link to post
Share on other sites
The execution time of a quicksort algorithm in javaUsing system date in java... How?

I have this quicksort algorithm and need to find its execution time using java eclipse, the output should be the sorted array and the time of execution. I would really appreciate your help.

 

public class QuickSort{  public static void main(String a[]){    int I;    int array[] = {12,9,4,99,120,1,3,10,13};    System.Out.Println(" Quick Sortandand");    System.Out.Println("Values Before the sort:and");    for(I = 0; I < array.Length; I++)      System.Out.Print( array+"  ");    System.Out.Println();    quick_srt(array,0,array.Length-1);    System.Out.Print("Values after the sort:and");    for(I = 0; I <array.Length; I++)      System.Out.Print(array+"  ");    System.Out.Println();    System.Out.Println("PAUSE");  }  public static void quick_srt(int array[],int low, int and){    int lo = low;    int hi = and;    if (lo >= and) {      return;    }    int mid = array[(lo + hi) / 2];    while (lo < hi) {      while (lo<hi && array[lo] < mid) {        lo++;      }      while (lo<hi && array[hi] > mid) {        hi--;      }      if (lo < hi) {        int T = array[lo];        array[lo] = array[hi];        array[hi] = T;      }    }    if (hi < lo) {      int T = hi;      hi = lo;      lo = T;    }    quick_srt(array, low, lo);    quick_srt(array, lo == low ? lo+1 : lo, and);  }}

-question by arli

Share this post


Link to post
Share on other sites
Current DateUsing system date in java... How?

Just run the below code, you will get current date : MM/dd/yyyyImport java.Text.SimpleDateFormat;Import java.Util.Date;Public class CurrentDate { public static void main(String[] args) { final SimpleDateFormat dtFormat = new SimpleDateFormat("MM/dd/yyyy"); System.Out.Println("Current Date: " + dtFormat.Format(new Date()).ToString()); }}Replying to suicide

-reply by Srikanth

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.