conversion of DATE&TMIE java [duplicate] - java

This question already has answers here:
Creating java date object from year,month,day
(6 answers)
Why is January month 0 in Java Calendar?
(18 answers)
Closed 10 months ago.
Trying to convert ist into cst but I'm not getting the expected output if, I passes the hardcoded value. Expected output is>
IST:: 18-04-2022 14:11:53
CST:: 18-04-2022 03:41:53
and output I am getting after hardcode value (one month ahead)
IST:: 18-05-2022 14:10:33
CST:: 18-05-2022 03:40:33
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class datetime {
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
Calendar Local = Calendar.getInstance();
Local.set(2022, 4, 18, 14, 10);
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String ist = formatter.format(Local.getTime());
System.out.println("IST:: " + ist);
Date theist = formatter.parse(ist);
//Convertion of ist into cst
TimeZone cst = TimeZone.getTimeZone("CST");
formatter.setTimeZone(cst);
String cst1 = formatter.format(theist.getTime());
System.out.println("CST:: "+ cst1);
}
}

Related

How to parse time stamp with GMT in string to date format? [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 5 years ago.
How to parse the following timestamp in string formate to date formate.
Mon Sep 25 13:40:56 GMT+05:30 2017
Since it has GMT in the timestamp so I didn't find a valid answer for this. Please let me know how to write SimpleDateFormat for this?
in java 7 with SimpleDateFormat
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
String input = "Mon Sep 25 13:40:56 GMT+05:30 2017";
String dateFormat = "EEE MMM d HH:mm:ss z yyyy";
try {
Date date = new SimpleDateFormat(dateFormat).parse(input);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
See it in action : https://ideone.com/RaCugz

SimpleDateFormat does not give correct date [duplicate]

This question already has answers here:
Java Date year calculation is off by year for two days
(5 answers)
Closed 5 years ago.
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class myTests {
public static void main(String[] args) {
DateFormat formatter = new SimpleDateFormat("YYYY-MMM-dd", Locale.US);
try {
Date sortingDate = (Date)formatter.parse("2017-Jul-13");
System.out.println("Sorted Date is:"+sortingDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Result is
Sorted Date is:Sun Jan 01 00:00:00 PST 2017
Why wont it show date i gave 2017 Jul 13
Can you please let me know.
Thanks
Abe
uppercase Y is Week year. What you Need is lowercase y = Year.
So Change new SimpleDateFormat("YYYY-MMM-dd", Locale.US); to new SimpleDateFormat("yyyy-MMM-dd", Locale.US); and you should get the correct result.
For more informations see the javadoc of SimpleDateFormat
If you are using java8, you should Change to DateTimeFormatter and the new DateTime API

java How do I convert a string that is FormatStyle.MEDIUM ("Nov 30, 2016") to a LocalDateTime type? [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
How can I parse/format dates with LocalDateTime? (Java 8)
(11 answers)
Closed 6 years ago.
In java if I have a String that looks like this: "Nov 30, 2016" (this is FormatStyle.MEDIUM), how do I convert it into a LocalDateTime data type?
I ran this on JDK 8:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class DateFormatDemo {
public static void main(String[] args) {
String dateStr = "Nov 30, 2016";
LocalDateTime ldt = LocalDateTime.parse(dateStr, DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
System.out.println("date string: " + dateStr);
System.out.println("local date : " + ldt);
}
}
Here's my output:
date string: Nov 30, 2016
local date : 2016-11-30
Process finished with exit code 0
I got an exception with the original code using LocalDateTime, because the input String didn't include a time.
If you'd like LocalDateTime, try adding hh:mm:ss to the input.

Java: Converting 12 hour time to 24 hour time [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
Is there a simple function to convert a time that's like this: 10:30pm 17 Aug 2016 NZST to 2016-8-17-2230
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String [] args) throws Exception {
SimpleDateFormat displayFormat = new SimpleDateFormat("yyyy-MM-dd-HHmm");
Date date = new Date();//Current Date
System.out.println(displayFormat.format(date));
}
}
See Java Documentation
http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html

Incorrect output after converting to SqlDate [duplicate]

This question already has answers here:
Convert Date to String using simpledateformat
(5 answers)
Closed 6 years ago.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
/**
* #param args
*/
public static void main(String[] args) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
String dateInString = "2016-04-23";
try {
Date date = formatter.parse(dateInString);
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
System.out.println("Date: -- "+date);
System.out.println("FormatterDate : -- "+formatter.format(date));
System.out.println("SQLDATE : -- "+sqlDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Output :
Date: -- Sat Jan 23 00:04:00 CST 2016
FormatterDate : -- 2016-04-23
SQLDATE : -- 2016-01-23
My out put should be 2016-04-23 not 2016-01-23
As per the javadocs
m Minute in hour
You want
M Month in year
Kindly note that mm stands for Minute and not Month. For month you have to use MM instead.

Categories

Resources