to get local Date...

B

Bumsys

I want to get local Date from windows. this date depends on what
windows is installed(English, German).
For Example: English windows time: 3/18/08 9:21 AM
German windows time: 18.03.08 09:22

How can I get this Date?
 
N

Nigel Wade

I want to get local Date from windows. this date depends on what
windows is installed(English, German).
For Example: English windows time: 3/18/08 9:21 AM

That would be American format, we English prefer our dates in sequential order
rather than inside-out order.
German windows time: 18.03.08 09:22

How can I get this Date?

Use DateFormat and Local.

For example:

package tests;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class FormatDate {
public static void main(String[] args) {
Locale american = new Locale("en", "US" );
Locale german = new Locale("de", "DE");

DateFormat americanFormat =
DateFormat.getDateInstance(DateFormat.SHORT, american);
DateFormat germanFormat =
DateFormat.getDateInstance(DateFormat.SHORT, german);

Date date = new Date();

System.out.println(americanFormat.format(date));
System.out.println(germanFormat.format(date));
}
}

will produce the following output:
3/18/08
18.03.08

As for getting the time, I'll leave that as an exercise for the student...
 
L

Lew

Nigel said:
That would be American format, we English prefer our dates in sequential order
rather than inside-out order.


Use DateFormat and Local.
^^^^^
Locale, as in java.util.Locale.

Actually /getting/ the date from Windows does not involve formats - you're
reading an integral quantity from the system clock. Time zones remain an
issue, but string formats are completely irrelevant.

/Displaying/ the date according to locale, or parsing a string that represents
a date, use the advice Nigel and others gave.
 
L

Lew

but i need also to have the time 09:22 not only date 18.03.08...

Read the docs. You have to be able to learn some things on your own - in this
case it's like asking for directions to the gas station that's a quarter mile
from the off-ramp after Nigel told you which exit to take off the highway.

He told you he left it "as an exercise for the student" - you don't want to
miss out on your exercise. It's good for your health.
 
R

Roedy Green

That would be American format, we English prefer our dates in sequential order
rather than inside-out order.

ISO dates are yyyy-mm-dd

Given that almost everything you write now adays could potentially be
read by people anywhere in the world, it seems to me we would should
be avoiding national date formats and using ISO.

National dates in isolation are ambiguous. They might be dd-mm or
mm-dd. ISO date format thankfully is still unambiguous.
 
R

Roedy Green

but i need also to have the time 09:22 not only date 18.03.08...

I already gave the answer to you. You ignored what I gave you
already. Presumably you believed it had no value without even looking
at it.

When someone ignores a gift you spent days preparing, you tend to
avoid helping that person again.

See http://mindprod.com/jgloss/newsgroups.html
for hints on getting the maximum benefit from your questions.
 
N

Nigel Wade

Roedy said:
ISO dates are yyyy-mm-dd

Given that almost everything you write now adays could potentially be
read by people anywhere in the world, it seems to me we would should
be avoiding national date formats and using ISO.

National dates in isolation are ambiguous.

Indeed. Trying to decide exactly what is meant by a date of the format 2/3/2008
is a big problem. Good software will use the locale, bad software will hardcode
the programmer's idea of the locale. But which did any particular piece of
software actually use?

One of the worst culprits I have to deal with is Veritas NetBackup, a rather
expensive enterprise backup system. Because it's American software all dates
are output in US format, even though the GUI interface is written in Java. It
must have taken them more work to hardcode the output format specifiers to US
than it would have done to use a Locale and its date formatters.
 
D

Dr J R Stockton

In comp.lang.java.programmer message <0jovt3hlq7vmvt8otii5gelhag8u8hiefu
@4ax.com>, Tue, 18 Mar 2008 15:43:04, Roedy Green <see_website@mindprod.
com.invalid> posted:
National dates in isolation are ambiguous. They might be dd-mm or
mm-dd. ISO date format thankfully is still unambiguous.

If one knows the string to be an ISO date, it is unambiguous. Extended
format - yyyy-mm-dd - could be an arithmetic expression. And it should
not be assumed that a date such as 20080305 is ISO Basic format (try
Google for that string).
 
D

Dr J R Stockton

In comp.lang.java.programmer message <[email protected]>,
Indeed. Trying to decide exactly what is meant by a date of the format 2/3/2008
is a big problem. Good software will use the locale, bad software will hardcode
the programmer's idea of the locale.

Good software will use either the locale or the ISO form for human I/O.

But, for communicating with other systems elsewhere, good software will
use a fixed, agreed, and preferably ISO format.

Nowadays, too, a single locale may not suit a given user (Locale was
probably introduced on the basis that people all use the same system
mm/dd/yy hh:mm am, except for foreigners).

But, hereabouts, while almost everyone uses GMT in winter and BST in
summer, hence the "proper" chronological locale, a very large proportion
would probably prefer Korean locale otherwise.
 
D

Dr J R Stockton

In comp.lang.java.programmer message <[email protected]
on.merlyn.invalid>, Wed, 19 Mar 2008 18:13:59, Dr J R Stockton
In comp.lang.java.programmer message <0jovt3hlq7vmvt8otii5gelhag8u8hiefu
@4ax.com>, Tue, 18 Mar 2008 15:43:04, Roedy Green <see_website@mindprod.
com.invalid> posted:


If one knows the string to be an ISO date, it is unambiguous. Extended
format - yyyy-mm-dd - could be an arithmetic expression. And it should
not be assumed that a date such as 20080305 is ISO Basic format (try
Google for that string).

Oops : I meant to write "for YYYYDDMM".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top