simpledateformat

Whats the difference in using a and aaa in SimpleDateFormat

寵の児 提交于 2019-12-10 11:32:42
问题 I want to display current date as 00:50:32 A Here is my code Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss a"); String time = sdf.format(date); System.out.println("time : " + time); But it print as: time : 00:50:32 AM I tried both HH:mm:ss a and HH:mm:ss aaa , but results are the same. 回答1: Can't do! If the pattern is 4 letters or less, short format is used. So 'a', 'aa', 'aaa' and 'aaaa' are identical. All you can do is to format it without 'a', and add 'A' or

Java convert millisecond timestamp to date with respect to given timezone

给你一囗甜甜゛ 提交于 2019-12-10 11:28:51
问题 I have seen many resources on this but one thing I cant get is when converting the millisecond timestamp, how I add a corresponding time zone, during the conversion process. Date date = new Date(Long.valueOf(dateInMil*1000L); SimpleDateFormat myDate = new SimpleDateFormat("EEE, MMM d, ''yy"); String formatted = myDate.format(date); Now if I have a time zone/offset in string formate i.e. "-04:00" or "+2:00" how to apply it to the above so I can get the proper date ? 回答1: I was doing a similar

(Simple)DateFormat that allow 24:00:00 and 00:00:00 as inputs

早过忘川 提交于 2019-12-10 04:22:20
问题 I've been looking for this for a while, with no success so far. Do you know if there's a "DateFormat" ish class, that will allow me to use "00:00:00" and "24:00:00" as input parameters (they're both midnight) but when called "getHour()" I'll get 0 or 24 as a result? Using "kk" will only allow me to have <1:24> range, meanwhile I'm looking for <0:24> range formatting 回答1: The value 24:00 is not represented in a LocalTime because it is strictly part of the next day. Consideration was given to

Parse some string date to another format

为君一笑 提交于 2019-12-09 23:18:07
问题 I have problem formating date. From : EEE, d MMM yyyy HH:mm:ss Z (example : Mon, 05 Jan 2014 15:10:00 +0200) To : dd/MMM/yyyy HH:mm (example : 05/01/2014 15:10) Here is what i tried : private String formatDate(String date) { SimpleDateFormat format = new SimpleDateFormat("dd/MMM/yyyy HH:mm"); Date dateResult = null; try { dateResult = format.parse(date); } catch (java.text.ParseException e) { Log.e(TAG, "", e); } return dateResult.toString(); } I get exception : unparseable date at offset 0

How to convert a UTC date format String to Date format in java?

做~自己de王妃 提交于 2019-12-09 19:29:09
问题 I have a requirement where the date is in UTC format like: Thu Jan 1 19:30:00 UTC+0530 1970 . I want to convert in to normal date format dd-MM-yyyy HH:mm:ss .Below is the code that i tried. DateFormat formatter = new SimpleDateFormat("E,MMM dd,yyyy h:mmaa"); String today = formatter.format("Thu Jan 1 19:30:00 UTC+0530 1970"); SimpleDateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); Date d = f.parse(masterDetailsJsonObject.get("cols1").toString()); But it throws an exception

Java中日期类型的处理

僤鯓⒐⒋嵵緔 提交于 2019-12-09 18:52:31
package servlet ; import org . junit . Test ; import java . text . ParseException ; import java . text . SimpleDateFormat ; public class DateTest { // sqlDate ---> utilDate // 无举例 --- 直接赋值即可(子类对象可以直接为父类引用赋值) @Test public void sqlDateToUtilDate ( ) { } // utilDate ---> sqlDate // ok @Test public void utilDateToSqlDate ( ) { java . util . Date utilDate = new java . util . Date ( ) ; System . out . println ( utilDate ) ; // Mon Dec 09 17:33:51 CST 2019 long timeLong = utilDate . getTime ( ) ; System . out . println ( timeLong ) ; // 1575884078639 13位 java . sql . Date sqlDate = new java . sql .

Unknown pattern character 'x', when using SimpleDateFormat

回眸只為那壹抹淺笑 提交于 2019-12-09 17:50:00
问题 Im trying to format a Date to String using SimpleDateFormat , and the pattern im using is this one "yyyy-MM-dd'T'HH:mm:ss.SSSxxx" but when reach this line SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSxxx"); i get the following exception: java.lang.IllegalArgumentException: Unknown pattern character 'x' at java.text.SimpleDateFormat.validatePatternCharacter(SimpleDateFormat.java:323) at java.text.SimpleDateFormat.validatePattern(SimpleDateFormat.java:312) at java.text

1.Java基本数据类型总结

时光毁灭记忆、已成空白 提交于 2019-12-09 16:29:24
Java基本数据类型总结 基本类型,或者叫做内置类型,是 Java 中不同于类的特殊类型。它们是我们编程中使用最频繁的类型。java是一种强类型语言,第一次申明变量必须说明数据类型,第一次变量赋值称为变量的初始化。 1. Java的简单类型及其封装器类 Java基本类型共有八种,基本类型可以分为三类,字符类型char,布尔类型boolean以及数值类型byte、short、int、long、float、double。数值类型又可以分为整数类型byte、short、int、long和浮点数类型float、double。JAVA中的数值类型不存在无符号的,它们的取值范围是固定的,不会随着机器硬件环境或者 操作系统 的改变而改变。实际上,JAVA中还存在另外一种基本类型void,它也有对应的包装类 java.lang.Void,不过我们无法直接对它们进行操作。8 种类型表示范围如下: byte: 8位,最大存储数据量是255,存放的数据范围是-128~127之间。 short: 16位,最大数据存储量是65536,数据范围是-32768~32767之间。 int: 32位,最大数据存储容量是2的32次方减1,数据范围是负的2的31次方到正的2的31次方减1。 long: 64位,最大数据存储容量是2的64次方减1,数据范围为负的2的63次方到正的2的63次方减1。 float: 32位

Java 中日期的几种常见操作 —— 取值、转换、加减、比较

可紊 提交于 2019-12-09 09:51:19
java 的开发过程中免不了与 Date 类型纠缠,准备总结一下项目经常使用的日期相关操作,JDK 版本 1.7,如果能够帮助大家节约那么几分钟起身活动一下,去泡杯咖啡,便是极好的,嘿嘿。当然,我只提供了可行的解决方案,并不保证是最佳实践,欢迎讨论。 1. 日期取值 在旧版本 JDK 的时代,有不少代码中日期取值利用了 java.util.Date 类,但是由于 Date 类不便于实现国际化,其实从 JDK1.1 开始,就更推荐使用 java.util.Calendar 类进行时间和日期方面的处理。这里便不介绍 Date 类的操作,让我们直奔主题吧,如何利用 Calendar 类取得现在的日期时间。 由于 Calendar 的构造器方法被 protected 修饰,所以我们会通过 API 中提供的 getInstance 方法来创建 Calendar 对象。 1 //有多个重载方法创建 Calendar 对象2 Calendar now = Calendar.getInstance(); //默认3 //指定时区和地区,也可以只输入其中一个参数4 Calendar now = Calendar.getInstance(timeZone, locale); 然后我们就可以通过该对象取得当前的各种时间参数了。 int year = now.get(Calendar.YEAR); int

How can I get yesterday's date without using Calendar in Java and without a timestamp just the date? [duplicate]

萝らか妹 提交于 2019-12-08 14:38:06
问题 This question already has answers here : How to check if a date Object equals yesterday? (8 answers) Closed 3 years ago . I have wrote a method to get the current date in the format of yyyy-MM-dd and want to be able to also create another method for getting yesterday's date, the day before the current date. All this needs is the date and not the timestamp. I am not trying to use Calendar as well. I have set up the current date this way: public class DateWithNoTimestamp { private static final