-
Notifications
You must be signed in to change notification settings - Fork 1
Date
SQL Error is 17268 : Year out of rangeRecent LastQuarter of a Minute- SimpleDateFormat is not thread safe. NumberFormatException while parsing string represented date to date object
- Get date base on day count: Using GregorianCalendar and simpleDateFormat
- JavaScript: Difference between two dates in years, months, days
Central European Time (CET), used in most parts of Europe and a few North African countries, is a standard time which is 1 hour ahead of Coordinated Universal Time (UTC). The time offset from UTC can be written as UTC+01:00.
European countries using CET in the winter and CEST in the summer:
Currently Central European Summer Time (CEST), UTC +2
Standard time (Central European Time (CET), UTC +1) starts October 25, 2020
CET alternates between UTC+1 (standard time) and UTC+2 (when daylight saving time (DST) is observed). All countries in the CET time zone observe DST (UTC+2) from 02:00 am on the last Sunday of March until 03:00 am on the last Sunday of October.
Time zone : https://time.is/CET
Specifies whether or not date/time interpretation is to be lenient. With lenient interpretation, a date such as "February 942, 1996" will be treated as being equivalent to the 941st day after February 1, 1996. With strict (non-lenient) interpretation, such dates will cause an exception to be thrown. The default is lenient.
dateFormat.setLenient(false);
Parameters:lenient true if the lenient mode is to be turned on; false if it is to be turned off.
CET = UTC+01:00 ShortDay(Gap), LongDay(Overlap)
TransitionRule[Gap +01:00 to , SUNDAY on or after MARCH 25 at 02:00 STANDARD, standard offset +01:00]
TransitionRule[Overlap +02:00 to +01:00, SUNDAY on or after OCTOBER 25 at 02:00 STANDARD, standard offset +01:00]
Example:
public class ShortDayTest {
public static void main(String[] args) throws ParseException {
String[] datesArr = {
"2020-03-29T01:00:00", "2020-03-29T02:00:00", "2020-03-29T03:00:00", "2020-03-29T04:00:00", // Short Day on 2nd hour
"2020-10-25T01:00:00", "2020-10-25T02:00:00", "2020-10-25T03:00:00" // Long Day on 2nd hour
};
String timeZone = System.getProperty("user.timezone");
System.out.println("Time Zone:"+timeZone);
//TimeZone.setDefault( TimeZone.getTimeZone("UTC") ); // Default: java.sql.Timestam. Change: GMT, CET, UTC
//System.setProperty("user.timezone", "UTC"); // or -Duser.timezone=GMT in the JVM args.
System.out.println("Zone System:"+ System.getProperty("user.timezone")+", TimeZone.getDefault().toZoneId(): "+ZoneId.systemDefault());
String formatStr = "yyyy-MM-dd'T'HH:mm:ss";
for (String strDate : datesArr) {
DateFormat dateFormat = new SimpleDateFormat(formatStr);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); // Provided date as CET, Converting corresponding Default UTC date
Date dates = dateFormat.parse(strDate);
String utcDate = dateFormat.format(dates);
/*SimpleTimeZone[id=Europe/Berlin,dstSavings=3600000,useDaylight=true]
1603591200000 : UTC TimeStamp: 2020-10-25 02:00:00.0
1603587600 : CET
3600 : dstSavings*/
Calendar cal = new GregorianCalendar();
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
cal.setTimeInMillis(dates.getTime());
DateFormat dateFormat22 = new SimpleDateFormat(formatStr);
dateFormat22.setTimeZone(TimeZone.getTimeZone("CET")); // UTC, -01:00, GMT, GMT-7, IST-"Asia/Kolkata"
Date dates22 = dateFormat22.parse(utcDate);
DateFormat dateFormat2 = new SimpleDateFormat(formatStr, Locale.ENGLISH);
dateFormat2.setTimeZone(TimeZone.getTimeZone("CET"));
Date dates2 = dateFormat2.parse(strDate);
displayKeyVal("Privided", strDate);
displayKeyVal("convertUTCtoCET", convertUTCtoCET(strDate) );
displayKeyVal("Locale.UTC", dates+"");
displayKeyVal("UTC Str", utcDate);
displayKeyVal("GMT String", dates.toGMTString());
displayKeyVal("Locale.CET", dates22+"");
displayKeyVal("Locale.ENGLISH", dates2+"");
displayKeyVal("UTC Time Default(CET)", new java.sql.Timestamp(dates.getTime())+"");
displayKeyVal("UTC - CET Time", new java.sql.Timestamp(dates22.getTime())+"");
displayKeyVal("ENGLISH Time", new java.sql.Timestamp(dates2.getTime())+"");
displayKeyVal("GregorianCalendar", cal.getTime()+"");
displayKeyVal("TimeStamp GC", new java.sql.Timestamp(cal.getTimeInMillis())+"");
displayKeyVal("TimeStamp Local", (getTimestamp(strDate)).toString());
System.out.println("======================");
}
/*String startDate = "2020-03-29T01:00:00";
String endDate = "2020-03-29T02:00:00";
compareTwoTimeStamps(startDate, endDate);
String startDate2 = "2020-03-29T03:00:00";
String endDate2 = "2020-03-29T04:00:00";
compareTwoTimeStamps(startDate2, endDate2);
System.out.println("------------------------------");
try {
lenientStrictParseDate(dateStr, "UTC");
} catch (ParseException e) {
e.printStackTrace();
}
try {
parseDate(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Instant instant = Instant.parse( dateStr + "Z"); // `Instant` is always in UTC.
java.util.Date date = java.util.Date.from( instant );
System.out.println("UTC : "+date);
String prop = System.getProperty("java.time.zone.DefaultZoneRulesProvider");
System.out.println("DefaultZoneRulesProvider : "+ prop);
displayZonedDate(dateStr, "UTC"); // UTC, GMT, UT
displayZonedDate(dateStr, "CET");
//displayZonedDate(dateStr, "CETDST");
// displayZoneRules(); */
}
public static Timestamp getTimestamp(String strDate) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(strDate, formatter);
//LocalDateTime.of(10000, Month.DECEMBER, 30, 12, 10, 05);
Timestamp valueOf = java.sql.Timestamp.valueOf(dateTime);
System.out.format("TimeStamp Local : [%-30s]\n", valueOf.toString());
java.sql.Date sqlDate = new java.sql.Date( valueOf.getTime() );
System.out.format("java.sql.Date : [%-30s]\n", sqlDate);
return valueOf;
/*LocalDateTime with = dateTime.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));
int dayOfMonth = with.getDayOfMonth();
System.out.println("Last Sunday of Month: "+dayOfMonth);*/
}
public static void displayKeyVal(String key, String val) {
System.out.format("[%-25s] : [%-35s]\n", key, val);
}
public static long compareTwoTimeStamps(String startDate, String endDate) throws ParseException {
Date start = parseDate(startDate);
Date end = parseDate(endDate);
java.sql.Timestamp startTime = getTimeStamp(start);
java.sql.Timestamp endTime = getTimeStamp(end);
long startTimeMilli = startTime.getTime();
long endTimeMilli = endTime.getTime();
long diff = endTimeMilli - startTimeMilli;
//long diffSeconds = diff / 1000;
//long diffMinutes = diff / (60 * 1000);
long diffHours = diff / (60 * 60 * 1000);
//long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.format("Start:%s, End:%s\n", start, end);
System.out.println("Difference in Hours : "+ diffHours);
if (diffHours >= 1) {
Timestamp oneHourAgo = new Timestamp(endTime.getTime() - (60 * 60 * 1000));
System.out.println("--- Before: "+ endTime.toLocaleString());
System.out.println("--- After: "+ oneHourAgo.toLocaleString());
}
return diffHours;
}
public static void displayZonedDate(String dateStr, String zone) {
Instant instant = Instant.parse( dateStr + "Z");
ZoneId zoneId = ZoneId.of( zone ); // Define a time zone rather than rely implicitly on JVM’s current default time zone.
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId ); // Assign a time zone adjustment from UTC.
java.util.Date dateZoned = java.util.Date.from( zdt.toInstant() );
System.out.println(zone+" : "+dateZoned);
}
public static Timestamp getTimeStamp(Date date) { //Date dbResponseTime = new java.util.Date();
Timestamp timestamp = new java.sql.Timestamp(date.getTime());
System.out.println("Timestamp :"+ timestamp);
return timestamp;
}
private static String convertUTCtoCET(String UTCDate) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = formatter.parse(UTCDate);
SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateValue = FORMATTER.format(date);
SimpleDateFormat DF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DF.setLenient(true);
DF.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat DF1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = DF.parse(dateValue);
DF1.setTimeZone(TimeZone.getTimeZone("CET"));
String CETDateFormat = DF1.format(startDate);
return CETDateFormat;
}
static String formatStr = "yyyy-MM-dd'T'HH:mm:ss";
public static Date lenientStrictParseDate(String aSource, String zone) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(formatStr);
dateFormat.setTimeZone(TimeZone.getTimeZone(zone));
//dateFormat.setLenient(false);
Date date = dateFormat.parse(aSource);
System.out.format(zone + " Old [%s]= New Date [%s]\n", aSource, date.toGMTString());
getTimeStamp(date);
return date;
}
public static Date parseDate(String aSource) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(formatStr, Locale.ENGLISH);
Date date = dateFormat.parse(aSource);
System.out.format("attribute [%s]= Date [%s]\n", aSource, date.toString());
getTimeStamp(date);
return date;
}
public static void displayZoneRules() {
ZoneId zone = ZoneId.of("CET");
System.out.println(zone);
System.out.println(zone.getRules());
for (ZoneOffsetTransition trans : zone.getRules().getTransitions()) {
System.out.println(trans);
}
for (ZoneOffsetTransitionRule rule : zone.getRules().getTransitionRules()) {
System.out.println(rule);
}
}
}In a class-based object-oriented language, in general, state is carried by instances, methods are carried by classes, and inheritance is only of structure and behavior. Basic, Refactoring Techniques
Method signature: It consists of method name and parameter list (number/type/order of the parameters). methodName(parametersList y). An instance method in a subclass with the same signature and return type as an instance method in the super-class overrides the super-class's method.
Java OOP concepts
Class - Collection of a common features of a group of object [static/instance Fields, blocks and Methods]
Object - Instance of a class (instance fields)
Abstraction - Process of hiding complex info and providing required info like API, Marker Interfaces ...
Encapsulation(Security) - Class Binding up with data members(fields) and member functions.
Inheritance (Reusability by placing common code in single class)
1. Multilevel - {A -> B -> C} 2. Multiple - Diamond problem {A <- (B) -> C} [Java not supports] 3. Cyclic {A <-> B} [Java not supports]
* Is-A Relation - Class A extends B
* Hash-A Relation - Class A { B obj = new B(); } - (Composition/Aggregation)
Polymorphism (Flexibility) 1. Compile-Time Overloading 2. Runtime Overriding [Greek - "many forms"]
int[] arr = {1,2,3}; int arrLength = arr.length; // Fixed length of sequential blocks to hold same data type
String str = "Yash"; int strLength = str.length(); // Immutable Object value can't be changed.
List<?> collections = new ArrayList<String>(); int collectionGroupSize = collections.size();
Map<?, ?> mapEntry = new HashMap<String, String>();
Set<?> keySet = mapEntry.keySet(); // Set of Key's
Set<?> entrySet = mapEntry.entrySet(); // Set of Entries [Key, Value]
// Immutable Objects once created they can't be modified. final class Integer/String/Employee
Integer val = Integer.valueOf("100"); String str2 = String.valueOf(100); // Immutable classes
final class Employee { // All Wrapper classes, java.util.UUID, java.io.File ...
private final String empName; // Field as Final(values can be assigned only once) Only getter functions.
public Employee(String name) { this.empName = name; }
} Native Java Code for Hashtable.h, Hashtable.cpp
SQL API.
You can check your current JDK and JRE versions on your command prompt respectively,
- JDK
javac -version [C:\Program Files\Java\jdk1.8.0_121\bin]o/p:javac 1.8.0_121 - JRE
java -version[C:\Program Files\Java\jdk1.8.0_121\bin]o/P:java version "1.8.0_102"
JAVA_HOME - Must be set to JDK otherwise maven projects leads to compilation error. [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? C:\Softwares\OpenJDK\, 7-zip
Fatal error compiling: invalid target release: JRE and JDK must be of same version
1.8.0.XXX
Disable TLS 1.0 and 1.1
security-libs/javax.net.ssl: TLS 1.0 and 1.1 are versions of the TLS protocol that are no longer considered secure and have been superseded by more secure and modern versions (TLS 1.2 and 1.3).
Core Java
-
Java Programming Language Basics
- Object, Class, Encapsulation, Interface, Inheritance, Polymorphism (Method Overloading, Overriding)
- JVM Architecture, Memory Areas
- JVM Class Loader SubSystem
- Core Java Interview Questions & Programs
- Interview Concepts
Stack Posts
- Comparable vs Comparator
- Collections and Arrays
-
String, StringBuffer, and StringBuilder
- String reverse
- Remove single char
- File data to String
- Unicode equality check Spacing entities
- split(String regex, int limit)
- Longest String of an array
-
Object Serialization
- Interface's Serializable vs Externalizable
- Transient Keyword
-
implements Runnablevsextends Thread - JSON
- Files,
Logging API- Append text to Existing file
- Counting number of words in a file
- Properties
- Properties with reference key