How to use java7 to get current Windows time format - java

Currently, both display language and format language of Windows 7 are English. So the short time format is "h:mm tt"
Run the following code:
Date today = Calendar.getInstance().getTime();
DateFormat timeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
String dateOut = timeFormatter.format(today);
System.out.println("short: " + dateOut);
Get the result:
short: 10:36 AM
Then change the short time format to "H:mm", run the code again, still get the result:
short: 10:36 AM
But what I expect is
short: 10:36
And I don't want to hard code the format pattern, it should be following the short time format changes.
Does anyone have any idea? Thanks a lot.

Try it:
Date today = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm");
String dateOut = dateFormat.format(today);
System.out.println("short: " + dateOut);

Related

Java how to Format Date to yyyy-MM-dd?

Hey there I have a question regarding the way that Java parses or formatts a Date.
I have this code:
private DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.FULL, Locale.GERMAN);
private DateFormat dateFormatter2 = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.GERMAN);
...
String dateTest = dateFormatter.format(Long.parseLong(pair.getKey().get(3)));
String dateTest2 = dateFormatter2.format(Long.parseLong(pair.getKey().get(3)));
System.out.println("dateTest: " + dateTest + " || dateTest2: " + dateTest2);
This gives me following result:
dateTest: Donnerstag, 2. Februar 2023 || dateTest2: 02.02.2023
Now I want to convert the date to this Format: "yyyy-MM-dd". I tried with simpledateformatter and the Parse function but always ended up in Errors like this:
java.text.ParseException: Unparseable date: "02.02.2023"
How can I simply change the date to my desired format? Would be cool if the result was of type Date.
DateFormatter only shows me how to do it from a Date but I have a String. The Problem is that I dont know how to change the String into a Date.
new Date(string) and (Date) string do not work.
Edit:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH);
LocalDate date = LocalDate.parse(dateTest2, formatter);
System.out.println("NewDate " + date);
result is:
SEVERE: Uncaught Exception
java.time.format.DateTimeParseException: Text '01.02.2023' could not be parsed at index 0
You probably need something like:
LocalDate date = Instant.ofEpochMilli(Long.parseLong(pair.getKey().get(3))).atZone(ZoneId.systemDefault()).toLocalDate();
System.out.println(date);
If you have to have Date, you can do:
Date d = Date.from(Instant.ofEpochMilli(Long.parseLong(pair.getKey().get(3))));
If you just need to get a java.util.Date from a long value which is the number of milliseconds since the Unix epoch, it really is trivial:
long millis = Long.parseLong(pair.getKey().get(3));
Date date = new Date(millis);
Or inlined if you really want to:
Date date = new Date(Long.parseLong(pair.getKey().get(3)));
I would strongly advise you to try to migrate all your code to java.time though. It's a far superior API.

How to get current time stamp in android?

I want to get current Timestamp in this YYYY-MM-DDTHHmmssZ .For instance; Expected value : 2016-01-01%2000:00:00. I have tried various methods,One of them is below :
String timeStamp = new SimpleDateFormat("YYYY-MM-DD'T'HH:mm:ss'Z'").format(new Timestamp(System.currentTimeMillis()));
Still I am facing the error (didn't getting expected output which is 2016-01-01%2000:00:00) , Any help will be appreciate,thanks.
just use this:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy-hh-mm-ss");
String format = simpleDateFormat.format(new Date());
Log.d("MainActivity", "Current Timestamp: " + format);
just change the format in which you want current time stamp.
Thanks. :)
Your question is ambiguous but according to my understanding:
String timeStamp = new SimpleDateFormat("YYYY-MM-DD'T'HH:mm:ss'Z'")
You haven to set the timezone, like:
private static SimpleDateFormat df
= new SimpleDateFormat( "yyyy-MM-dd'T'hh:mm:ssz")
df.setTimeZone(TimeZone.getTimeZone("GMT"));
OR
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sdf.format(new Date())); //-prints-> 2015-01-22T03:23:26Z
If you want to learn more. please refer to:
https://svn.apache.org/repos/asf/commons/dormant/feedparser/trunk/src/java/org/apache/commons/feedparser/tools/ISO8601DateParser.java

Conversion of UTC to IST time in java is working in LOCAL but not in CLOUD SERVER

I am working in date conversion in java in that i am using following code snippet to convert the UTC time to IST format.It is working properly in the local when i run it but when i deploy it in server its not converting , its displaying only the utc time itself.Is there any configuaration is needed in server side.Please help me out.
CODE SNIPPET:
DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String pattern = "dd-MM-yyyy HH:mm:ss";
SimpleDateFormat formatter;
formatter = new SimpleDateFormat(pattern);
try {
String formattedDate = formatter.format(utcDate);
Date ISTDate = sdf.parse(formattedDate);
String ISTDateString = formatter.format(ISTDate);
return ISTDateString;
}
Java Date objects are already/always in UTC. Time Zone is something that is applied when formatting to text. A Date cannot (should not!) be in any time zone other than UTC.
So, the entire concept of converting utcDate to ISTDate is flawed.
(BTW: Bad name. Java conventions says it should be istDate)
Now, if you want the code to return the date as text in IST time zone, then you need to request that:
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata")); // Or whatever IST is supposed to be
return formatter.format(utcDate);
Using Java 8 New API,
Instant s = Instant.parse("2019-09-28T18:12:17Z");
ZoneId.of("Asia/Kolkata");
LocalDateTime l = LocalDateTime.ofInstant(s, ZoneId.of("Asia/Kolkata"));
System.out.println(l);

PHP timestamp to Java timestamp

I need to convert this 1384174174 timestamp from PHP to Java. This is how I echo the date('Y/m/d H:i:s' ,$dn1['timestamp'] in PHP yet I don't know how to do it in Java. Please help me. Thanks.
In Java, you'd do it like this:
Date date = new Date(1384174174);
SimpleDateFormat f = new SimpleDateFormat("Y/M/d H:m:s");
String dateFormatted = f.format(date);
Watch out for the format pattern where, unlike PHP, M is month in year and m is minute in hour.
This method will take your Unix style date and create a Java Date. It returns a readable string. It should help you get started.
private String unixToString(long unixSeconds) {
Date date = new Date(unixSeconds);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}

Expected Lenient behaviour in java date-related classes (JodaTime and/or Java.Date)

I have the following situation:
To plan an activity that started on a particular date and ends on the next day, an end-date/time value is provided exceeding the 24u hour constraint.
For e.g: start-date = 2013-01-01 23:22:00 and end-date = 2013-01-01 26:22:00
I thought the solution was, using the lenient methods provided by JodaTime. But it behaved not the way I expected. So I provided some test-code to, explain my confusion:
public static void main(String[] args){
String lenientDate = "2013-01-01 26:22:00";
String format = "YYYY-MM-DD hh:mm:ss";
try{
System.out.println("Lenient Date:" + lenientDate);
DateFormat sdf = new SimpleDateFormat(format);
sdf.setLenient(true);
System.out.println("Java.Date output:" + sdf.parse(lenientDate));
DateTime dt = new DateTime(sdf.parse(lenientDate));
System.out.println("hybrid calls:" + dt.toString());
DateTimeFormatter parseFormat = DateTimeFormat.forPattern(format);
Chronology lenient = LenientChronology.getInstance(GregorianChronology.getInstance());
parseFormat = parseFormat.withChronology(lenient);
DateTime dateTime = parseFormat.parseDateTime(lenientDate);
System.out.println("All Joda calls:" + dateTime.toString());
System.out.println("My expected values: " + dateTime.minusHours(12).toString());
}catch(Exception e){
System.out.println("Something went wrong!");
}
}
And the outcome is:
Lenient Date:2013-01-01 26:22:00
Java.Date output:Mon Dec 31 02:22:00 CET 2012
hybrid calls:2012-12-31T02:22:00.000+01:00
All Joda calls:2013-01-02T14:22:00.000+01:00
My expected values: 2013-01-02T02:22:00.000+01:00
Library: Joda 2.2; Java: JDK 1.7; Platform: Mac OSX
Can somebody explain the right behaviour of Leniency in date/time conversions?
You have invalid date pattern at "hh:mm:ss". Date format should be:
String format = "yyyy-MM-dd HH:mm:ss";
DateTimeFormat documentation
h - clockhour of halfday (1~12)
H - hour of day (0~23)
With this date pattern all examples works as expected

Categories