Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- === LAB.3 PICERIJA ===
- import java.util.Scanner;
- import java.util.*;
- class InvalidExtraTypeException extends Exception{
- public InvalidExtraTypeException(String message){
- super(message);
- }
- }
- class InvalidPizzaTypeException extends Exception{
- public InvalidPizzaTypeException(String message){
- super(message);
- }
- }
- class ItemOutOfStockException extends Exception{
- public ItemOutOfStockException(Item item){
- }
- }
- class ArrayIndexOutOfBoundsException extends Exception{
- public ArrayIndexOutOfBoundsException(int i){}
- }
- class EmptyOrderException extends Exception{
- public EmptyOrderException(String message){
- super(message);
- }
- }
- class OrderLockedException extends Exception{
- }
- interface Item {
- public int getPrice();
- public String getType();
- }
- class ExtraItem implements Item{
- String type;
- public ExtraItem(String _type) throws InvalidExtraTypeException{
- if( _type.equals("Ketchup") || _type.equals("Coke"))
- type = _type;
- else
- throw new InvalidExtraTypeException(_type);
- }
- public int getPrice(){
- if (type.equals("Ketchup")) return 3;
- if (type.equals("Coke")) return 5;
- return 0;
- }
- public String getType(){
- return type;
- }
- }
- class PizzaItem implements Item{
- String type;
- public PizzaItem(String _type) throws InvalidPizzaTypeException{
- if(_type.equals("Standard") || _type.equals("Pepperoni") || _type.equals("Vegetarian"))
- type = _type;
- else
- throw new InvalidPizzaTypeException(_type);
- }
- public int getPrice(){
- if (type.equals("Standard")) return 10;
- if (type.equals("Pepperoni")) return 12;
- if (type.equals("Vegetarian")) return 8;
- return 0;
- }
- public String getType(){
- return type;
- }
- }
- class Order{
- List<Item> list;
- List<Integer> listCount;
- boolean locked = false;
- public Order(){
- list = new ArrayList<Item>();
- listCount = new ArrayList<Integer>();
- }
- public void addItem(Item item, int count) throws Exception{
- if (locked == true) throw new OrderLockedException();
- if (count > 10) throw new ItemOutOfStockException(item);
- int index = -1;
- for(int i = 0; i < list.size(); i++){
- if(list.get(i).getType().equals(item.getType())){
- index = i;
- break;
- }
- }
- if ( index == -1 ){
- list.add(item);
- listCount.add(count);
- }
- else {
- listCount.set(index, count);
- }
- }
- public int getPrice(){
- int sum = 0;
- for (int i = 0; i < list.size(); i++){
- sum += list.get(i).getPrice()*listCount.get(i);
- }
- return sum;
- }
- public void displayOrder(){
- for(int i = 0; i < list.size(); i++){
- System.out.format("%3d.%-15sx%2d%5d$\n",i+1,list.get(i).getType(),listCount.get(i).intValue(),listCount.get(i).intValue()*list.get(i).getPrice());
- }
- System.out.format("%-22s%5d$\n","Total:",this.getPrice());
- }
- public void lock() throws EmptyOrderException {
- try{
- if (list.size() == 0) throw new EmptyOrderException("EmptyOrder");
- locked = true;
- }catch(Exception e){
- System.out.println(e.getMessage());
- }
- }
- public void removeItem(int index) throws Exception{
- if (locked == true) throw new OrderLockedException();
- if(index < 0 || index > list.size()-1) throw new ArrayIndexOutOfBoundsException(index);
- list.remove(index);
- listCount.remove(index);
- }
- }
- public class PizzaOrderTest {
- public static void main(String[] args) {
- Scanner jin = new Scanner(System.in);
- int k = jin.nextInt();
- if (k == 0) { //test Item
- try {
- String type = jin.next();
- String name = jin.next();
- Item item = null;
- if (type.equals("Pizza")) item = new PizzaItem(name);
- else item = new ExtraItem(name);
- System.out.println(item.getPrice());
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- }
- if (k == 1) { // test simple order
- Order order = new Order();
- while (true) {
- try {
- String type = jin.next();
- String name = jin.next();
- Item item = null;
- if (type.equals("Pizza")) item = new PizzaItem(name);
- else item = new ExtraItem(name);
- if (!jin.hasNextInt()) break;
- order.addItem(item, jin.nextInt());
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- }
- jin.next();
- System.out.println(order.getPrice());
- order.displayOrder();
- while (true) {
- try {
- String type = jin.next();
- String name = jin.next();
- Item item = null;
- if (type.equals("Pizza")) item = new PizzaItem(name);
- else item = new ExtraItem(name);
- if (!jin.hasNextInt()) break;
- order.addItem(item, jin.nextInt());
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- }
- System.out.println(order.getPrice());
- order.displayOrder();
- }
- if (k == 2) { // test order with removing
- Order order = new Order();
- while (true) {
- try {
- String type = jin.next();
- String name = jin.next();
- Item item = null;
- if (type.equals("Pizza")) item = new PizzaItem(name);
- else item = new ExtraItem(name);
- if (!jin.hasNextInt()) break;
- order.addItem(item, jin.nextInt());
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- }
- jin.next();
- System.out.println(order.getPrice());
- order.displayOrder();
- while (jin.hasNextInt()) {
- try {
- int idx = jin.nextInt();
- order.removeItem(idx);
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- }
- System.out.println(order.getPrice());
- order.displayOrder();
- }
- if (k == 3) { //test locking & exceptions
- Order order = new Order();
- try {
- order.lock();
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- try {
- order.addItem(new ExtraItem("Coke"), 1);
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- try {
- order.lock();
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- try {
- order.removeItem(0);
- } catch (Exception e) {
- System.out.println(e.getClass().getSimpleName());
- }
- }
- }
- }
- === LAB.3 TELEFONSKI IMENIK ===
- import java.util.*;
- import java.io.*;
- //21:59
- class InvalidNumberException extends Exception{
- public InvalidNumberException() {
- super("InvalidNumberException");
- }
- public InvalidNumberException(String message) {
- super(message);
- }
- }
- class InvalidNameException extends Exception{
- String name;
- public InvalidNameException(String name) {
- super("InvalidNameException");
- this.name = name;
- }
- }
- class MaximumSizeExceddedException extends Exception{
- public MaximumSizeExceddedException() {
- super("MaximumSizeExceddedException");
- }
- public MaximumSizeExceddedException(String message) {
- super(message);
- }
- }
- class InvalidFormatException extends Exception{
- public InvalidFormatException() {
- super("InvalidFormatException");
- }
- public InvalidFormatException(String message) {
- super(message);
- }
- }
- class Contact{
- protected String name;
- protected String [] phones;
- public Contact(String _name, String ..._phonenumber) throws InvalidNumberException, InvalidNameException, MaximumSizeExceddedException{
- name = setName(_name);
- if (_phonenumber.length > 5)
- throw new MaximumSizeExceddedException();
- phones = new String [_phonenumber.length];
- for (int i = 0; i < _phonenumber.length; i++){
- phones[i] = setPhone(_phonenumber[i]);
- }
- }
- public Contact(Contact c){
- name = new String (c.name);
- phones = new String[c.phones.length];
- for (int i=0; i<c.phones.length; ++i)
- phones[i] = new String (c.phones[i]);
- }
- private String setPhone(String phonenumber) throws InvalidNumberException{
- if(phonenumber.length() != 9) throw new InvalidNumberException();
- for(int i = 0; i < phonenumber.length(); i++){
- if(!Character.isDigit(phonenumber.charAt(i)))
- throw new InvalidNumberException();
- }
- if (phonenumber.charAt(0) != '0' || phonenumber.charAt(1) != '7' || phonenumber.charAt(2) == '3' ||
- phonenumber.charAt(2) == '4' || phonenumber.charAt(2) == '9')
- throw new InvalidNumberException();
- return phonenumber;
- }
- private String setName(String name) throws InvalidNameException{
- if(name.length()<=4 || name.length()>10) throw new InvalidNameException(name);
- for(int i = 0; i <name.length();i++){
- if( !Character.isAlphabetic(name.charAt(i))&&!Character.isDigit(name.charAt(i))) throw new InvalidNameException(name);
- }
- return name;
- }
- public String getName(){return name;}
- public String [] getNumbers(){
- String [] ret = new String [phones.length];
- for(int i = 0;i < phones.length; i++){
- ret[i] = new String (phones[i]);
- }
- Arrays.sort(ret);
- return ret;
- }
- public void addNumber(String phonenumber) throws MaximumSizeExceddedException, InvalidNumberException{
- if(phones.length == 5) throw new MaximumSizeExceddedException();
- String [] temp = new String [phones.length + 1];
- for(int i = 0; i <phones.length; i++)
- temp[i] = phones[i];
- temp[phones.length] = setPhone(phonenumber);
- phones = temp;
- }
- public String toString(){
- String ret = name+"\n"+phones.length+"\n";
- String [] temp = new String [phones.length];
- for(int i = 0;i < phones.length; i++){
- temp[i] = new String (phones[i]);
- }
- Arrays.sort(temp);
- for(int i = 0; i < phones.length; i++)
- ret += temp[i]+"\n";
- return ret;
- }
- }
- class PhoneBook{
- List<Contact> contacts;
- public PhoneBook(){
- contacts = new ArrayList<Contact>();
- }
- public void addContact(Contact contact) throws MaximumSizeExceddedException, InvalidNameException{
- if(contacts.size() == 250) throw new MaximumSizeExceddedException();
- for(int i = 0; i < contacts.size(); i++){
- if(contacts.get(i).getName().equals(contact.getName())) throw new InvalidNameException(contact.getName());
- }
- contacts.add(contact);
- }
- public Contact getContactForName(String name){
- for(int i = 0; i < contacts.size(); i++){
- if(contacts.get(i).getName().equals(name))
- return contacts.get(i);
- }
- return null;
- }
- public int numberOfContacts(){
- return contacts.size();
- }
- public Contact[] getContacts(){
- Contact [] temp = new Contact [contacts.size()];
- for (int i = 0; i <contacts.size(); i++)
- temp[i] = new Contact (contacts.get(i));
- Arrays.sort(temp, new Comparator<Contact>(){
- @Override
- public int compare(Contact o1, Contact o2){
- if (o1.getName().compareTo(o2.getName()) < 0)
- return -1;
- else if (o1.getName().compareTo(o2.getName()) > 0)
- return 1;
- return 0;
- }
- });
- return temp;
- }
- public boolean removeContact(String name){
- for (int i = 0;i < contacts.size(); i++){
- if(contacts.get(i).getName().equals(name)){
- contacts.remove(i);
- return true;
- }
- }
- return false;
- }
- public String toString(){
- StringBuilder sb = new StringBuilder();
- Contact [] temp = this.getContacts();
- for (Contact nowCon : temp)
- sb.append(nowCon.toString()).append("\n");
- return sb.toString();
- }
- public static boolean saveAsTextFile(PhoneBook phonebook, String path){
- boolean ret = true;
- try{
- PrintWriter print = new PrintWriter(new FileOutputStream(path));
- print.print(phonebook.toString());
- print.flush();
- print.close();
- }catch(FileNotFoundException e){
- ret = false;
- }
- return ret;
- }
- public static PhoneBook loadFromTextFile(String path) throws FileNotFoundException, IOException, InvalidFormatException{
- try{
- BufferedReader bf = new BufferedReader(new FileReader(path));
- String line = bf.readLine();
- PhoneBook pb = new PhoneBook();
- while ( line != null ) {
- String name = line;
- line = bf.readLine();
- int n = Integer.parseInt(line);
- String [] phone = new String[n];
- for (int i=0; i<n; ++i) {
- phone[i] = bf.readLine();
- }
- Contact c = new Contact(name,phone);
- pb.addContact(c);
- line = bf.readLine();
- }
- return pb;
- }catch (InvalidNameException e) {
- throw new InvalidFormatException();
- } catch (MaximumSizeExceddedException e) {
- throw new InvalidFormatException();
- } catch (InvalidNumberException e) {
- throw new InvalidFormatException();
- }
- }
- public Contact[] getContactsForNumber(String number_prefix) {
- Contact [] forn = new Contact[contacts.size()];
- int k =0;
- for (int i=0; i<contacts.size(); ++i) {
- for (int j=0; j<contacts.get(i).getNumbers().length; ++j) {
- if (contacts.get(i).getNumbers()[j].startsWith(number_prefix)) {
- forn[k] = new Contact(contacts.get(i));
- ++k;
- break;
- }
- }
- }
- Contact [] help = new Contact[k];
- for (int i=0; i<k; ++i) {
- help[i] = new Contact(forn[i]);
- }
- return help;
- }
- }
- public class PhonebookTester {
- public static void main(String[] args) throws Exception {
- Scanner jin = new Scanner(System.in);
- String line = jin.nextLine();
- switch( line ) {
- case "test_contact":
- testContact(jin);
- break;
- case "test_phonebook_exceptions":
- testPhonebookExceptions(jin);
- break;
- case "test_usage":
- testUsage(jin);
- break;
- }
- }
- private static void testFile(Scanner jin) throws Exception {
- PhoneBook phonebook = new PhoneBook();
- while ( jin.hasNextLine() )
- phonebook.addContact(new Contact(jin.nextLine(),jin.nextLine().split("\\s++")));
- String text_file = "phonebook.txt";
- PhoneBook.saveAsTextFile(phonebook,text_file);
- PhoneBook pb = PhoneBook.loadFromTextFile(text_file);
- if ( ! pb.equals(phonebook) ) System.out.println("Your file saving and loading doesn't seem to work right");
- else System.out.println("Your file saving and loading works great. Good job!");
- }
- private static void testUsage(Scanner jin) throws Exception {
- PhoneBook phonebook = new PhoneBook();
- while ( jin.hasNextLine() ) {
- String command = jin.nextLine();
- switch ( command ) {
- case "add":
- phonebook.addContact(new Contact(jin.nextLine(),jin.nextLine().split("\\s++")));
- break;
- case "remove":
- phonebook.removeContact(jin.nextLine());
- break;
- case "print":
- System.out.println(phonebook.numberOfContacts());
- System.out.println(Arrays.toString(phonebook.getContacts()));
- System.out.println(phonebook.toString());
- break;
- case "get_name":
- System.out.println(phonebook.getContactForName(jin.nextLine()));
- break;
- case "get_number":
- System.out.println(Arrays.toString(phonebook.getContactsForNumber(jin.nextLine())));
- break;
- }
- }
- }
- private static void testPhonebookExceptions(Scanner jin) {
- PhoneBook phonebook = new PhoneBook();
- boolean exception_thrown = false;
- try {
- while ( jin.hasNextLine() ) {
- phonebook.addContact(new Contact(jin.nextLine()));
- }
- }
- catch ( InvalidNameException e ) {
- System.out.println(e.name);
- exception_thrown = true;
- }
- catch ( Exception e ) {}
- if ( ! exception_thrown ) System.out.println("Your addContact method doesn't throw InvalidNameException");
- /*
- exception_thrown = false;
- try {
- phonebook.addContact(new Contact(jin.nextLine()));
- } catch ( MaximumSizeExceddedException e ) {
- exception_thrown = true;
- }
- catch ( Exception e ) {}
- if ( ! exception_thrown ) System.out.println("Your addContact method doesn't throw MaximumSizeExcededException");
- */
- }
- private static void testContact(Scanner jin) throws Exception {
- boolean exception_thrown = true;
- String names_to_test[] = { "And\nrej","asd","AAAAAAAAAAAAAAAAAAAAAA","�ндрејA123213","Andrej#","Andrej<3"};
- for ( String name : names_to_test ) {
- try {
- new Contact(name);
- exception_thrown = false;
- } catch (InvalidNameException e) {
- exception_thrown = true;
- }
- if ( ! exception_thrown ) System.out.println("Your Contact constructor doesn't throw an InvalidNameException");
- }
- String numbers_to_test[] = { "+071718028","number","078asdasdasd","070asdqwe","070a56798","07045678a","123456789","074456798","073456798","079456798" };
- for ( String number : numbers_to_test ) {
- try {
- new Contact("Andrej",number);
- exception_thrown = false;
- } catch (InvalidNumberException e) {
- exception_thrown = true;
- }
- if ( ! exception_thrown ) System.out.println("Your Contact constructor doesn't throw an InvalidNumberException");
- }
- String nums[] = new String[10];
- for ( int i = 0 ; i < nums.length ; ++i ) nums[i] = getRandomLegitNumber();
- try {
- new Contact("Andrej",nums);
- exception_thrown = false;
- } catch (MaximumSizeExceddedException e) {
- exception_thrown = true;
- }
- if ( ! exception_thrown ) System.out.println("Your Contact constructor doesn't throw a MaximumSizeExceddedException");
- Random rnd = new Random(5);
- Contact contact = new Contact("Andrej",getRandomLegitNumber(rnd),getRandomLegitNumber(rnd),getRandomLegitNumber(rnd));
- System.out.println(contact.getName());
- System.out.println(Arrays.toString(contact.getNumbers()));
- System.out.println(contact.toString());
- contact.addNumber(getRandomLegitNumber(rnd));
- System.out.println(Arrays.toString(contact.getNumbers()));
- System.out.println(contact.toString());
- contact.addNumber(getRandomLegitNumber(rnd));
- System.out.println(Arrays.toString(contact.getNumbers()));
- System.out.println(contact.toString());
- }
- static String[] legit_prefixes = {"070","071","072","075","076","077","078"};
- static Random rnd = new Random();
- private static String getRandomLegitNumber() {
- return getRandomLegitNumber(rnd);
- }
- private static String getRandomLegitNumber(Random rnd) {
- StringBuilder sb = new StringBuilder(legit_prefixes[rnd.nextInt(legit_prefixes.length)]);
- for ( int i = 3 ; i < 9 ; ++i )
- sb.append(rnd.nextInt(10));
- return sb.toString();
- }
- }
- === LAB.4 LOCALDATE ===
- import java.sql.Date;
- import java.sql.Timestamp;
- import java.time.*;
- import java.time.temporal.TemporalAdjusters;
- /**
- * LocalDate test
- */
- public class LocalDateTest {
- public static void main(String[] args) {
- System.out.println(create());
- System.out.println(parse());
- System.out.println(with().getYear());
- System.out.println(withAdjuster());
- System.out.println(plus());
- System.out.println(minus());
- System.out.println(plusPeriod());
- System.out.println(isAfter());
- System.out.println(until());
- }
- static LocalDate create() {
- LocalDate ret = LocalDate.of(2015, 6, 18);
- return ret;
- }
- static LocalDate parse() {
- LocalDate ret = LocalDate.parse("2015-06-18");
- return ret;
- }
- static LocalDate with() {
- LocalDate ld = DateAndTimes.LD_20150618;
- LocalDate ret = ld.with(ld);
- return ret;
- }
- static LocalDate withAdjuster() {
- LocalDate ld = DateAndTimes.LD_20150618;
- LocalDate ret = ld.with(TemporalAdjusters.firstDayOfNextYear());
- return ret;
- }
- static LocalDate plus() {
- LocalDate ld = DateAndTimes.LD_20150618;
- LocalDate ret = ld.plusMonths(10);
- return ret;
- }
- static LocalDate minus() {
- LocalDate ld = DateAndTimes.LD_20150618;
- LocalDate ret = ld.minusDays(10);
- return ret;
- }
- static LocalDate plusPeriod() {
- LocalDate ld = DateAndTimes.LD_20150618;
- LocalDate ret = ld.plusDays(3).plusMonths(2).plusYears(1);
- return ret;
- }
- static boolean isAfter() {
- LocalDate ld = DateAndTimes.LD_20150618;
- LocalDate ld2 = DateAndTimes.LD_20150807;
- boolean ret = ld2.isAfter(ld);
- return ret;
- }
- static Period until() {
- LocalDate ld = DateAndTimes.LD_20150618;
- LocalDate ld2 = DateAndTimes.LD_20150807;
- Period ret = ld.until(ld2);
- return ret;
- }
- }
- class DateAndTimes {
- public static final LocalDate LD_20150618 = LocalDate.of(2015, 6, 18);
- public static final LocalDate LD_20150807 = LocalDate.of(2015, 8, 7);
- }
- === LAB.4 LOCALTIME ===
- import java.time.Duration;
- import java.time.LocalTime;
- import java.time.temporal.ChronoField;
- import java.time.temporal.ChronoUnit;
- /**
- * LocalTime API tests
- */
- public class LocalTimeTest {
- public static void main(String[] args) {
- System.out.println(localTimeOfHourToMinute());
- System.out.println(localTimeOfHourToNanoSec());
- System.out.println(localTimeParse());
- System.out.println(localTimeWith());
- System.out.println(localTimePlus());
- System.out.println(localTimeMinus());
- System.out.println(localTimeMinusDuration());
- System.out.println(localDateIsBefore());
- System.out.println(localTimeTruncatedTo());
- }
- static LocalTime localTimeOfHourToMinute() {
- LocalTime ret = LocalTime.of(23,7);
- return ret;
- }
- static LocalTime localTimeOfHourToNanoSec() {
- LocalTime ret = LocalTime.of(23,7,3,100000000);
- return ret;
- }
- static LocalTime localTimeParse() {
- LocalTime ret = LocalTime.parse("23:07:03.1");
- return ret;
- }
- static LocalTime localTimeWith() {
- LocalTime lt = DateAndTimes.LT_23073050;
- LocalTime ret = lt.withHour(21);
- return ret;
- }
- static LocalTime localTimePlus() {
- LocalTime lt = DateAndTimes.LT_23073050;
- LocalTime ret = lt.plusMinutes(30);
- return ret;
- }
- static LocalTime localTimeMinus() {
- LocalTime lt = DateAndTimes.LT_23073050;
- LocalTime ret = lt.minusHours(3);
- return ret;
- }
- static LocalTime localTimeMinusDuration() {
- LocalTime lt = DateAndTimes.LT_23073050;
- /**
- * Define a {@link Duration} of 3 hours 30 minutes and 20.2 seconds
- * Create a {@link LocalTime} subtracting the duration from {@link lt} by using {@link LocalTime#minus}
- */
- Duration dur = Duration.ofHours(3).plusMinutes(30).plusSeconds(20).plusNanos(200000000);
- LocalTime ret = lt.minus(dur);
- return ret;
- }
- static boolean localDateIsBefore() {
- LocalTime lt = DateAndTimes.LT_23073050;
- LocalTime lt2 = DateAndTimes.LT_12100000;
- boolean ret = lt2.isBefore(lt);
- return ret;
- }
- static LocalTime localTimeTruncatedTo() {
- LocalTime lt = DateAndTimes.LT_23073050;
- LocalTime ret = lt.truncatedTo(ChronoUnit.MINUTES);
- return ret;
- }
- static class DateAndTimes {
- public static final LocalTime LT_23073050 = LocalTime.of(23, 7, 30, 500000000);
- public static final LocalTime LT_12100000 = LocalTime.of(12, 10);
- }
- }
- === LAB.4 LOCALDATETIME ===
- import java.time.LocalDate;
- import java.time.LocalDateTime;
- import java.time.LocalTime;
- import java.time.format.DateTimeFormatter;
- import java.time.temporal.ChronoUnit;
- import java.time.temporal.TemporalAdjusters;
- /**
- * LocalDateTime tests
- */
- public class LocalDateTimeTest {
- public static void main(String[] args) {
- System.out.println(localDateTimeOf());
- System.out.println(localDateTimeParse());
- System.out.println(localTimeWith());
- System.out.println(localDatePlusMinus());
- System.out.println(localDateTimeFormat());
- System.out.println(toLocalDateAndTime());
- System.out.println(toLocalDateTime());
- }
- static LocalDateTime localDateTimeOf() {
- LocalDateTime ret = LocalDateTime.of(2015,6,20,23,7,30);
- return ret;
- }
- static LocalDateTime localDateTimeParse() {
- LocalDateTime ret = LocalDateTime.parse("2015-06-20T23:07:30");
- return ret;
- }
- static LocalDateTime localTimeWith() {
- LocalDateTime ldt = DateAndTimes.LDT_20150618_23073050;
- LocalDateTime ret = ldt.with(TemporalAdjusters.firstDayOfNextMonth()).truncatedTo(ChronoUnit.HOURS);
- return ret;
- }
- static LocalDateTime localDatePlusMinus() {
- LocalDateTime ldt = DateAndTimes.LDT_20150618_23073050;
- LocalDateTime ret = ldt.plus(10, ChronoUnit.MONTHS).minus(5, ChronoUnit.HOURS);
- return ret;
- }
- static String localDateTimeFormat() {
- LocalDateTime ldt = DateAndTimes.LDT_20150618_23073050;
- String ret = ldt.format(DateTimeFormatter.ofPattern("YYYY_MM_d_HH_mm_ss"));
- return ret;
- }
- static String toLocalDateAndTime() {
- LocalDateTime ldt = DateAndTimes.LDT_20150618_23073050;
- LocalDate localDate = ldt.toLocalDate();
- LocalTime localTime = ldt.toLocalTime();
- return localDate.toString() + localTime.toString();
- }
- static String toLocalDateTime() {
- LocalDate ld = DateAndTimes.LD_20150618;
- LocalTime lt = DateAndTimes.LT_23073050;
- /**
- * Create two equal {@link LocalDateTime} from {@link ld} and {@link lt}
- * by using {@link LocalDate#atTime} and {@link LocalTime#atDate}
- */
- LocalDateTime localDateTime1 = lt.atDate(ld);
- LocalDateTime localDateTime2 = ld.atTime(lt);
- return localDateTime1.toString() + " " + localDateTime2.toString();
- }
- static class DateAndTimes {
- public static final LocalDate LD_20150618 = LocalDate.of(2015, 6, 18);
- public static final LocalTime LT_23073050 = LocalTime.of(23, 7, 30, 500000000);
- public static final LocalDateTime LDT_20150618_23073050 = LocalDateTime.of(2015, 6, 18, 23, 7, 30, 500000000);
- }
- }
- === LAB.4 ZONEDDATETIME ===
- import java.time.LocalDateTime;
- import java.time.ZoneId;
- import java.time.ZonedDateTime;
- import java.time.format.DateTimeFormatter;
- /**
- * ZonedDateTime tests
- */
- public class ZonedDateTimeTest {
- public static void main(String[] args) {
- System.out.println(zonedDateTimeOf());
- System.out.println(zonedDateTimeParse());
- System.out.println(zonedDateTimeFormat());
- System.out.println(toPST());
- System.out.println(sameInstantAs());
- System.out.println(sameLocalAs());
- }
- static ZonedDateTime zonedDateTimeOf() {
- ZonedDateTime ret = ZonedDateTime.of(2015,7,10,2,14,25,0,ZoneId.of("Asia/Tokyo"));
- return ret;
- }
- static ZonedDateTime zonedDateTimeParse() {
- ZonedDateTime ret = ZonedDateTime.parse("2015-06-18T23:07:25.000+09:00[Asia/Tokyo]");
- return ret;
- }
- static String zonedDateTimeFormat() {
- ZonedDateTime zdt = DateAndTimes.ZDT_20150618_23073050;
- String ret = zdt.format(DateTimeFormatter.ofPattern("YYYY_MM_d_HH_mm_s_z"));
- return ret;
- }
- static ZonedDateTime toPST() {
- LocalDateTime ldt = DateAndTimes.LDT_20150618_23073050;
- ZonedDateTime ret = ldt.atZone(ZoneId.of("America/Los_Angeles"));
- return ret;
- }
- static ZonedDateTime sameInstantAs() {
- ZonedDateTime zdt = DateAndTimes.ZDT_20150618_23073050;
- ZonedDateTime ret = zdt.withZoneSameInstant(ZoneId.of("America/Los_Angeles"));
- return ret;
- }
- static ZonedDateTime sameLocalAs() {
- ZonedDateTime zdt = DateAndTimes.ZDT_20150618_23073050;
- ZonedDateTime ret = zdt.withZoneSameLocal(ZoneId.of("America/Los_Angeles"));
- return ret;
- }
- static class DateAndTimes {
- public static final LocalDateTime LDT_20150618_23073050 = LocalDateTime.of(2015, 6, 18, 23, 7, 30, 500000000);
- public static final ZonedDateTime
- ZDT_20150618_23073050 = ZonedDateTime.of(LDT_20150618_23073050, ZoneId.of("Asia/Tokyo"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement