I'm developing an Android app that gets the date from the internet. What I have is a complete date format like this one : 2020-06-13T16:21:15.239920+02:00. I want to get the day of the month (which is 13 in this case).
If you are using at least API level 26, then you can use ZonedDateTime class as your string uses the default format that is understood by that class.
ZonedDateTime zdt = ZonedDateTime.parse("2020-06-13T16:21:15.239920+02:00");
int d = zdt.getDayOfMonth();
Alternatively, if the format is constant, simply use method substring()
String dd = "2020-06-13T16:21:15.239920+02:00".substring(8, 10);
If the format is not constant, I would suggest either regular expression or combining ZonedDateTime with DateTimeFormatter
Your date/time string complies with DateTimeFormatter.ISO_OFFSET_DATE_TIME. You can use java.time API to get the day of the month as shown below:
import java.time.OffsetDateTime;
public class Main {
public static void main(String[] args) {
String dateTimeStr = "2020-06-13T16:21:15.239920+02:00";
OffsetDateTime odt = OffsetDateTime.parse(dateTimeStr);
int day = odt.getDayOfMonth();
System.out.println(day);
}
}
Output:
13
If you can not use Java SE 8 Date and Time, check ThreeTenABP and also How to use ThreeTenABP in Android Project.
Its always a bad idea to split a string and extract data becuase the string will change to some other value once the date , month or any part of the date string changes and the string indexing will change
So use DateTimeFormatter
import java.time.MonthDay;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
public class SDF {
public static void main(String[] args) {
final TemporalAccessor parse = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSxxx")
.parse("2020-06-13T16:21:15.239920+02:00");
int dayOfMonth = MonthDay.from(parse).getDayOfMonth();
System.out.println(dayOfMonth);
}
}
There should be a method getDayOfMonth() available on this variable
private int parseDate(ZonedDateTime date) {
int day = date.getDayOfMonth();
return day;
}
You can use String split. Just take this whole format of date and put it into String and then create a new String Array String[] array= str.split("[-T]"); and then you can get result from array[2].
String str = "2020-06-13T16:21:15.239920+02:00";
String[] array= str.split("[-T]");
System.out.println("OUTPUT: " + array[2]);
Output: 13
String str = "2020-06-13T16:21:15.239920+02:00";
String substr = "";
// prints the substring after index 7(8 includes) till index 9(10 excludes)
substr = str.substring(8, 10);
Related
This question already has answers here:
How to convert a String of format yyyymmdd to LocalDate in JodaTime [duplicate]
(1 answer)
Change date format in a Java string
(22 answers)
Good way to convert integer YYYYMMDD into java.util.Date with local time zone
(4 answers)
Closed 3 years ago.
I am trying to process the data from a weather station. I simply want to process and print (for now) the minimum and maximum temperatures each day starting from 2000/01/01 until today 2019/12/12. The weather station gives me the date in an integer yyyymmdd like 20191212 or 20030317. I store this, alongside the minimum and maximum temperatures in an integer array, of about 21000 rows long... I want the date to be displayed in yyyy/mm/dd, like 2019/12/12 or 2003/03/17. How exactly do I go about doing this?
This is my code
import java.io.*;
public class Main {
public static void main(String args[]) {
int rowAmount = 7152; //the amount of rows in the document
int[] temperatures = new int[rowAmount*3]; //makes an array 3 times the size of rowAmount, to fit date, min temp and max temp.
try {
BufferedReader br = new BufferedReader(new FileReader("D:\\20002019minmaxtempbilt.txt")); // Makes the filereader
String fileRead = br.readLine(); // reads first like
int counter = 0; //sets a counter
while (fileRead != null) { // loops until the file ends.
String[] tokenize = fileRead.split(","); //splits the line in 3 segements, date, min temp and max temp. And stores them in following variables
int tempDate = Integer.parseInt(tokenize[0]);
int tempMin = Integer.parseInt(tokenize[1]);
int tempMax = Integer.parseInt(tokenize[2]);
//adds the values to the array
temperatures[counter] = tempDate;
counter++;
temperatures[counter] = tempMin;
counter++;
temperatures[counter] = tempMax;
}
// close file stream
br.close();
}
catch (FileNotFoundException fnfe)
{
System.out.println("file not found");
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
// Displays the entire array, formatted by date neatly, hopefully.
for(int i = 0; i<rowAmount; i =+ 3) {
int tempDate = temperatures[i];
int tempMin = temperatures[i+1];
int tempMax = temperatures[i+2];
drawLine(tempDate, tempMin, tempMax);
}
}
public static void drawLine(int tempDate, int tempMin, int tempMax) {
/*This is where I get stuck. I need to convert tempDate from an int yyyymmdd to either
3 separate integers representing year, month and day; or I need to convert it to a
string that can be printed out yyyy/mm/dd.*/
System.out.printf("");
}
}
Since your date is a String to start with I see no reason to convert it to an Integer first. Using the more current java.time package you can use one formatter to convert from a String to LocalDate and then another to convert back to a String with the right format,
DateTimeFormatter inFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate date = LocalDate.parse(tokenize[0], inFormatter);
DateTimeFormatter outFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
String outStr = outFormatter.format(date);
Since DateTimeFormatter is costly to initialise I would recommend you create both of them before the loop you have for reading the file.
Another way: if the integer value representing the date is stored in an int called date (example value: 20191212), then
int day = date % 100;
int month = (date/ 100) % 100;
int year = date / 10000;
then you can use a Formatter to format the string output that you want.
A problem is that an integer of 20191212 really doesn't represent a date.
I would recommend NOT transforming tempDate into an integer and leaving it as a string.
Edit
Here is an example that uses the Java 8 time package classes: LocalDate, DateTimeFormatter, and DateTimeFormatterBuilder:
package stackoverflow;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
public class ParseStringDate {
public static void main(String... args) {
String dateString = "20191231";
DateTimeFormatter dateTimeFormatterParser = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate localDate = LocalDate.parse(dateString, dateTimeFormatterParser);
System.out.println(localDate);
DateTimeFormatter dateTimeFormatterPrinter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
System.out.println(localDate.format(dateTimeFormatterPrinter));
dateTimeFormatterPrinter = new DateTimeFormatterBuilder()
.appendPattern("yyyy")
.appendLiteral("/")
.appendPattern("MM")
.appendLiteral("/")
.appendPattern("dd").toFormatter();
System.out.println(localDate.format(dateTimeFormatterPrinter));
}
}
Here is an example that uses SimpleDateFormat:
package stackoverflow;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormattingFromString {
public static void main(String... args) throws Exception {
String tempDate = "20191212";
SimpleDateFormat sdf1 = new SimpleDateFormat("YYYYmmdd");
Date date = sdf1.parse(tempDate);
// Now format the above date as needed...
SimpleDateFormat sdf2 = new SimpleDateFormat("YYYY/mm/dd");
System.out.println(sdf2.format(date));
}
}
As pointed out in the comments, SimpleDateFormat and the Date classes are not super great to work with. They are mutable and therefore not thread safe. The new java.time package classes are immutable and therefore thread safe. The new classes are also easier to do date math with and comparisons.
I'd recommend using a DateFormat object, taking advantage of the parse(String) and format(Date) methods.
I have a string "2017-01-03T02:20:52+00:00" I want to convert into a LocalDateTime.
I tried the code below
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World");
String date = "2009-07-16T19:20:30-05:00";
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime dateTime = LocalDateTime.parse(date, inputFormatter);
System.out.println(dateTime);
}
}
I tried various pattern but every time I am getting below error:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2009-07-16T19:20:30-05:00' could not be parsed at index 19
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at HelloWorld.main(HelloWorld.java:11)
A single Z does not allow : in the timezone. Use ZZZZZ (five Z) for the extended format.
The format of your date String is incorrect
Change it like so:
String date = "2009-07-16T19:20:30-0500";
Note the change before the timezone.
If you want to keep your date format, you can use this pattern:
String pattern = "yyyy-MM-dd'T'HH:mm:ssXXX";
try this:
String date = "2009-07-16T19:20:30-0500";
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
or
String date = "2009-07-16T19:20:30-05:00";
String pattern = "yyyy-MM-dd'T'HH:mm:ssz";
I have a text box for time that takes input as String and I have to convert it to a format that has a space between the number and AM or PM.
Eg: 5:00PM has to be converted into 5:00 PM
Here is an example using in Date/Time formatting features.
Note that it will throw parse errors if the input is not in the correct format.
If the AM/PM segment is guaranteed to be upper case the toUpperCase() call can be removed.
package com.stackoverflow.q42913242;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
#SuppressWarnings("javadoc")
public class Answer {
static final DateTimeFormatter FROM_PATTERN = DateTimeFormatter.ofPattern("h:mma");
static final DateTimeFormatter TO_PATTERN = DateTimeFormatter.ofPattern("h:mm a");
public static void main(String[] args) {
System.out.println(convert("5:00pm"));
System.out.println(convert("12:00AM"));
}
public static String convert(String input) {
final LocalTime localTime = LocalTime.parse(input.toUpperCase(), FROM_PATTERN);
return localTime.format(TO_PATTERN);
}
}
You can use substring for it, eg.:
String time = "5:00PM";
String newTime = time.substring(0,time.length()-2) + " " + time.substring(time.length()-2);
I have a string which can contain a date(yyyy-MM-dd) or date and time (yyyy-MM-dd HH:mm:ss) in respective formats.
I want to know which strings contains only date.
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dateFormat.parse("2015-02-02"));
System.out.println(dateFormat.parse("2015-02-02 23:23:23"));
In above code, both the strings are parsed successfully, whereas the format is same for only first.
I would use the overload of parse which takes a ParsePosition - you can then check the position afterwards:
import java.util.*;
import java.text.*;
public class Test {
public static void main(String[] args) throws Exception {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
System.out.println(parseFully(dateFormat, "2015-02-02"));
System.out.println(parseFully(dateFormat, "2015-02-02 23:23:23"));
}
private static Date parseFully(DateFormat format, String text)
throws ParseException {
ParsePosition position = new ParsePosition(0);
Date date = format.parse(text, position);
if (position.getIndex() == text.length()) {
return date;
}
if (date == null) {
throw new ParseException("Date could not be parsed: " + text,
position.getErrorIndex());
}
throw new ParseException("Date was parsed incompletely: " + text,
position.getIndex());
}
}
public static void main(String[] args) {
String dateOnly = "2015-02-02";
String dateAndTimeOnly = "2015-02-02 23:23:23";
System.out.println("Date Only = " + validateDateFormat(dateOnly));
System.out.println("Date And time Only = " + validateDateFormat(dateAndTimeOnly));
}
public static boolean validateDateFormat(String input) {
return input.matches("([0-9]{4})-([0-9]{2})-([0-9]{2})");
}
output
Date Only = true
Date And time Only = false
Regex is self explanatory - Input will be separated by -, ist part([0-9]{4}) can contain 4 digit , 2nd part can contain 2 digit [0-9]{2}, so as 3rd.
java.time
The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Solution using java.time, the modern Date-Time API:
Let's first try to do it the way you have done:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String[] arr = { "2015-02-02", "2015-02-02 23:23:23" };
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd", Locale.ENGLISH);
for (String s : arr) {
System.out.println("Attempting to parse '" + s + "':");
LocalDate date = LocalDate.parse(s, dtf);
System.out.println("Parsed successfully: " + date);
}
}
}
Output:
Attempting to parse '2015-02-02':
Parsed successfully: 2015-02-02
Attempting to parse '2015-02-02 23:23:23':
Exception in thread "main" java.time.format.DateTimeParseException: Text '2015-02-02 23:23:23' could not be parsed, unparsed text found at index 10
As you can see, the java.time API correctly throws an exception informing you about the problem. SimpleDateFormat, on the other hand, parses the input string silently which has caused the problem that you have posted.
Thus, with the modern date-time API, you have two easy options:
Simply catch the exception and say that the second input (i.e. 2015-02-02 23:23:23) is not a date string as per the specified date pattern.
Use the function, DateTimeFormatter#parse(CharSequence, ParsePosition) with the ParsePosition index set to 0.
Given below is a demo of the second option:
import java.text.ParsePosition;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String[] arr = { "2015-02-02", "2015-02-02 23:23:23" };
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd", Locale.ENGLISH);
for (String s : arr) {
ParsePosition pp = new ParsePosition(0);
LocalDate.from(dtf.parse(s, pp));
if (pp.getIndex() < s.length()) {
System.out.println("'" + s + "' is not a date string as per the specified date pattern.");
}
}
}
}
Output:
'2015-02-02 23:23:23' is not a date string as per the specified date pattern.
ONLINE DEMO
Note: Never use SimpleDateFormat or DateTimeFormatter without a Locale.
Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Once the desired format is reached , SimpleDateFormat doesnt format the rest of String . It is the reason why your second string is parsed.
This post SimpleDateFormat parse(string str) doesn't throw an exception when str = 2011/12/12aaaaaaaaa? may help you .
Also check the DateFormat#parse method in java docs
please tell me how to parse this date: "29-July-2012"
I try:
new SimpleDateFormat("dd-MMM-yyyy");
but it doesn't works. I get the following exception:
java.text.ParseException: Unparseable date: "29-July-2012"
You need to mention the Locale as well...
Date date = new SimpleDateFormat("dd-MMMM-yyyy", Locale.ENGLISH).parse(string);
In your String, the full format is used for month, so according to http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html you should be using MMMM as suggested in Baz's comment.
The reason for this can be read from the API docs.
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#month states that for month it will be interpreted as text if there are more than 3 characters and
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#text states that the full form (in your case 'July' rather than 'Jul') will be used for 4 or more characters.
Try this (Added Locale.ENGLISH parameter and long format for month)
package net.orique.stackoverflow.question11815659;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class Question11815659 {
public static void main(String[] args) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMMM-yyyy",
Locale.ENGLISH);
System.out.println(sdf.parse("29-July-2012"));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Use the split() function with the delimiter "-"
String s = "29-July-2012";
String[] arr = s.split("-");
int day = Integer.parseInt(arr[0]);
String month = arr[1];
int year = Integer.parseInt(arr[2]);
// Now do whatever u want with the day, month an year values....
Create a StringTokenizer. You first need to import the library:
import Java.util.StringTokenizer;
Basically, you need to create a delimeter, which is basically something to seperate the text. In this case, the delimeter is the "-" (the dash/minus).
Note: Since you showed the text with quotations and said parse, i'm assuming its a string.
Example:
//Create string
String input = "29-July-2012";
//Create string tokenizer with specified delimeter
StringTokenizer st = new StringTokenizer(input, "-");
//Pull data in order from string using the tokenizer
String day = st.nextToken();
String month = st.nextToken();
String year = st.nextToken();
//Convert to int
int d = Integer.parseInt(day);
int m = Integer.parseInt(month);
int y = Integer.parseInt(year);
//Continue program execution